Sort files by date in filename

Hi, I am a newbie to shell programming and I need some help in sorting a list of files in ascending order of date in the filenames.

The file format is always : IGL01_AC_D_<YYYYMMDD>_N01_01
For example, in a directory MyDirectory I have the following files:

IGL01_AC_D_20110712_N01_01.dat
IGL01_AC_D_20110615_N01_01.dat
IGL01_AC_D_20110914_N01_01.dat

I want to sort and process them in the following ascending order:

IGL01_AC_D_20110615_N01_01.dat
IGL01_AC_D_20110712_N01_01.dat
IGL01_AC_D_20110914_N01_01.dat

Can anyone please help me in achieving this?

I have try with ls comand which only sort the files according to the modification and creating date that UNIX assign to them but that not my goal. The goal is to sort by the date in the filename.
Thanking you in advance.

Note that given the filename format the shell will sort the filenames as you want
(just try executing ls -1 *.dat or printf '%s\n' *.dat ).

1 Like

Hello radoulov,
Thank you very much for your quick reply.
I have tested the code in your reply on my vmware machine for some files in the same format and indeed that really work!. :slight_smile:

I will integrate this piece of code in my script tomorrow and test the whole code on my real UNIX machine.

---------- Post updated at 03:21 PM ---------- Previous update was at 03:07 PM ----------

Can you please advise how should the command be if for example the files are different like for example:

IGL01_AC_D_20110615_N01_01.dat
IGL01_AC_D_20110712_N01_02.dat 
IGL01_AC_D_20110914_N01_01.dat
IGL01_AC_D_20110702_N01_01.dat
IGL01_AC_D_20110302_N01_02.dat  

and still i want to sort them by the date in the filename
Thank you.

printf '%s\n' *.dat | sort -t_ -k4,4
1 Like

Many many thanks to you. That was the way I play with your above reply:
Infact on production the filename has the following format:

IGL01_AC_D_<YYYYMMDD>_<N01>_<01>.dat where all the paramaters in angular brakets can be changed. N01 becomes N02 etc, 01 becomes 02 03 etc and
<YYYYMMDD> also.
From your code, I undertstood that -k4,4 is the position in the filename on which I want to sort..am i right?

I used your above logic to sort the file on the three columns with :

ls IGL01*.dat | sort -t_ -k4,4 -k5,5 -k6,6

Works pretty good..thank you again very much :slight_smile:

Yes,
it works like this.

By the way, -k4,4 -k5,5 ... should be the default behavior,
thus in this case -k4 should be sufficient.

1 Like

So basically, you mean that even if I have for example the following files:

IGL01_AC_D_20110615_N01_01.dat
IGL01_AC_D_20110614_N02_01.dat
IGL01_AC_D_20110614_N01_01.dat
IGL01_AC_D_20110614_N01_02.dat
ls IGL01*.dat | sort -t_ -k4 

will give the following result?

IGL01_AC_D_20110614_N01_01.dat
IGL01_AC_D_20110614_N01_02.dat
IGL01_AC_D_20110614_N02_01.dat
IGL01_AC_D_20110615_N01_01.dat

Yes. In addition, as long as the ASCIIbetical order is good for you,
you don't even need the sort command:

zsh-4.3.14[t]% ls -1 IGL01*.dat 
IGL01_AC_D_20110614_N01_01.dat
IGL01_AC_D_20110614_N01_02.dat
IGL01_AC_D_20110614_N02_01.dat
IGL01_AC_D_20110615_N01_01.dat
1 Like

woohooo..thats really amazing. :slight_smile:
I will surely play around with all these combinations of command to get a good feel.

Thank you lots very much. Nice to have people like you for helping us in these forums :slight_smile:

---------- Post updated 12-28-11 at 01:46 AM ---------- Previous update was 12-27-11 at 05:31 PM ----------

Sorry to be here again..if im not too abusing on this forum :slight_smile:

One question pop up in my mind last night.

what is the difference between the

-k4

and

-k4,4

Many Thanks

 
man sort

There is no substitute for testing things out yourself. Have a go and see what you get. It is simple enough to create a directory for testing and then use touch to create a bunch of files with the name in the correct format with enough variations to have a go with the code you have.

If there is more required, please show what you have tried and what your files are listed as and what you are trying to acheive. There are some subtle differences in the version of sort on some flavours of unix, so we might be breaking its rules somewhere.

Let us know how you get on.

Robin
Liverpool/Blackburn
UK

I will try some and get back to you but nevertherless I'm really satisfied with the answers given in the forums here because the modification I made to my shell works well and is already on prod env :slight_smile:

Big applause and thanks to you all. I will play around with some more command and get back soon. :slight_smile: