Sort By Date and Time

Hi ,

I would like to list or sort by date and time (the files are named in day and time format) where the latest file will be placed at the bottom and the earliest file be placed at the top. Can anybody help me?

My files are named in the following manner.

EG: abc_071128_144121_data
" abc " is random , " 071128 " depicts the date in yymmdd format, " 144121 " depicts the time in the hhmmss format

Input:
abc_071128_144121_data
xxx_071128_123524_data
fff_071128_122837_data
jjj_071129_010544_data
qqq_071129_002603_data
hhh_071128_120728_data
bbb_071128_113329_data

Ouput:
bbb_071128_113329_data
hhh_071128_120728_data
fff_071128_122837_data
xxx_071128_123524_data
abc_071128_144121_data
qqq_071129_002603_data
jjj_071129_010544_data

It's a bit (very) cheap but you could try this:

#!/bin/sh
for file in *
do
    echo $file | cut -d '_' -f 2,3,4 >> /tmp/sort.$$
done
for file in `sort -n < /tmp/sort.$$`
do
    ls -1 *_${file}
done
rm -f /tmp/sort.$$

Actually, cleaner would be:

ls -1 * | awk 'BEGIN { FS="_" } { print $2,$3,$4,$1}' | sort -n | awk 'BEGIN { OFS="_" } { print $4,$1,$2,$3}'

The following works for the dataset provided:

sort -t'_' -k2 -k3  datasetfile

Have not tested it on boundary cases.

Just to throw this out there...if the files are created in a specific time order irrespective of name, then the ls -t command would work nicely. With Solaris, the last modification time is used, but the -u option would use the file's access time.

Check your local man pages.

Hi,

Try sort as follow:

sort -t "_" +1 filename

Thanks all for the codes.
It's working now !! :smiley:

Hi Smiling Dragon,

I am using your code now.

ls -1 * | awk 'BEGIN { FS="_" } { print $2,$3,$4,$1}' | sort -n | awk 'BEGIN { OFS="_" } { print $4,$1,$2,$3}'

But now i have another problem, some of the filenames come in the form of " zzz_071128_144121_data_interim ", how can i sort it out by modifying your code? Example below

Input:
abc_071128_144121_data
xxx_071128_123524_data
fff_071128_122837_data
jjj_071129_010544_data
qqq_071129_002603_data
hhh_071128_120728_data
bbb_071128_113329_data
zzz_071128_163017_data_interim

Ouput:
bbb_071128_113329_data
hhh_071128_120728_data
fff_071128_122837_data
xxx_071128_123524_data
abc_071128_144121_data
zzz_071128_163017_data_interim
qqq_071129_002603_data
jjj_071129_010544_data

Why don't you use the above code - its simple, efficient, easy to maintain & isn't affected by other fields.

Hi rikxik,

Tried the code but encounter some errors. Can you give me some advice?

$ sort -t'_' -k2 -k3 *
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

$ ls -l |sort -t'_' -k2 -k3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

Works fine for me:

Sample:

$ ls -ltr
total 0
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 xxx_071128_123524_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 qqq_071129_002603_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 jjj_071129_010544_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 hhh_071128_120728_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 fff_071128_122837_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 bbb_071128_113329_data
-rw-r-----   1 sdass    informix       0 Jan  2 09:11 abc_071128_144121_data

Output:

$ ls -1 |sort -t'_' -k2 -k3
bbb_071128_113329_data
hhh_071128_120728_data
fff_071128_122837_data
xxx_071128_123524_data
abc_071128_144121_data
qqq_071129_002603_data
jjj_071129_010544_data

Do you know how '|' works or I am getting blind here?

Hi rikxik,

I am using solaris by the way.
This code's not working for me. :frowning:
Error message is telling me to input an argument for " k ". Do you have any idea on that ?

$ ls -l
total 0
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 abc_071128_144121_data
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 bbb_071128_113329_data
-rw-r--r-- 1 raynon real 6 Jan 2 17:06 zzz_071128_163017_data_interim

$ ls -1 |sort -t'_' -k2 -k3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

$ ls -1 |sort -t"_" -k2 -k3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

And I'm using sunOS - so shouldn't be that different:

Try this:

$ ls -1 |sort -t'_' -k2,2 -k3,3
bbb_071128_113329_data
hhh_071128_120728_data
fff_071128_122837_data
xxx_071128_123524_data
abc_071128_144121_data
qqq_071129_002603_data
jjj_071129_010544_data

This syntax should address your sort's crib.

HTH

Hi rikxik,

It still doesn't work. Can you help ?

$ ls -l
total 0
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 abc_071128_144121_data
-rw-r--r-- 1 raynon real 4 Jan 2 17:06 bbb_071128_113329_data
-rw-r--r-- 1 raynon real 6 Jan 2 17:06 zzz_071128_163017_data_interim

$ ls -1 |sort -t'_' -k2,2 -k3,3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

$ ls -1 |sort -t"_" -k2,2 -k3,3
sort: option requires an argument -- k
sort [-bcdfiMmnru] [-o output] [-T directory] [-ykmem] [-t char]
[+pos1 [-pos2]] [-k field_start[type][,field_end[type]] [file...]

Hi Raynon,
I have seen your query posted for sort date and time.

Can you pls explain me what does it mean?

ls -1 |sort -t'_' -k2,2 -k3,3

Pls tell me which field or column it is taking and sort. I was trying with many inputs but i didnt get the correct answer.

Thanks
Yamini

Quite strange - the command I posted is in accordance with the syntax your sort is throwing out.

Ok - first do this and see if you get filename listing (I can't believe I have to do this!):

$ ls -1
abc_071128_144121_data
bbb_071128_113329_data
fff_071128_122837_data
hhh_071128_120728_data
jjj_071129_010544_data
qqq_071129_002603_data
xxx_071128_123524_data

Once the above is verified, try this:

ls -1 |sort -t'_' +1 +2

@Yamini
sort -t'_' -k2,2 -k3,3 means first sort on starting field 2 and ending field 2 i.e. effectively on field 2. Then similarly on field 3.

HTH

rikxik and Raynon,

Please post the results of "which sort" and, Raynon, please also post the results of "uname -a" as rikxik did. This might help you guys get on the same page.

Yeah :slight_smile:

$ uname -a
SunOS db012a 5.8 Generic_117350-35 sun4us sparc FJSV,GPUZC-M
$ which sort
/usr/bin/sort

This is the usage sort mentions:

usage: sort [-cmu] [-o output] [-T directory] [-S mem] [-z recsz]
        [-dfiMnr] [-b] [-t char] [-k keydef] [+pos1 [-pos2]] files...

I think that alternate sort programs may be in /usr/xpg and/or /usr/ucb but I don't have a Solaris box real handy to check.

Works for xpg4 version too:

$ ls -1 |/usr/xpg4/bin/sort -t'_' -k1 -k2
abc_071128_144121_data
bbb_071128_113329_data
fff_071128_122837_data
hhh_071128_120728_data
jjj_071129_010544_data
qqq_071129_002603_data
xxx_071128_123524_data