Sorting based on File name

Hi All

I have a requirement to list all the files in chronological order based on the date value in the file name.For ex if I have three files as given below

ABC_TEST_20160103_1012.txt
ABC_TEST_20160229_1112.txt
ABC_TEST_20160229_1112.txt

I have written code as given below to list out the files.This is the command which I am trying to execute from Data stage job which is running on UNIX OS.

ls -m ABC*|sort -t_ -k3

The above code is working in this scenario.But in case if we receive a file name as below, this will not work, I need to change the command accordingly,as date is coming as first portion of the file name.

20160122_2130_ABC_TEST.txt

I want to write a common command in such a way that it will sort automatically based on the date value in the file name, irrespective of the position where the date value is coming in the file

Try something like:

ls | sed 's/.*\([0-9]\{8\}\)/\1 &/' | sort -k1,1n | cut -c10-

This assumes that 8 digits always constitute a date..

--
If the date is always followed by an underscore you could make this a bit more precise like so:

ls | sed 's/.*\([0-9]\{8\}\)_/\1 &/' | sort -k1,1n | cut -c10-
1 Like

And if the filenames always contain an 8 digit date followed by an underscore and a 4 digit time ( YYYYMMDD_hhmm ) and the desire is to sort on the date and time, you could try:

ls | sed 's/.*\([0-9]\{8\}_[0-9]\{4\}\)/\1 &/' | sort -k1,1 | cut -c14-

Note that I removed the sort -n option because an alphanumeric sort will get the desired results on the 1st pass while a numeric sort would stop comparing at the underscore (ignoring the timestamp on the 1st pass).

2 Likes