Sort date time in ascending order

Hi,

i had a data block (coming from pipe from other codes) as:

H  YF_CO.dat  77164  11/17/2013 04:00:02  731374590.96  1  1  731374590.96  76586  77164  578  2988  Y
H  YF_CO.dat  77164  11/17/2013 04:00:07  731374590.96  1  4  731374590.96  76586  77164  578  2988  Y
H  YF_CO.dat  77178  11/17/2013 04:00:07  731374590.96  2  4  731374590.96  76600  77178  578  3002  Y
H  YF_CO.dat  77178  11/17/2013 04:00:08  731374590.96  2  4  731374590.96  76600  77178  578  3002  Y
H  YF_CO.dat  77178  11/17/2013 04:15:11  731374590.96  2  4  731374590.96  76600  77178  578  3002  Y
H  YF_CO.dat  77195  11/16/2013 04:15:20  731374590.96  3  4  731374590.96  76617  77195  578  3019  Y
H  YF_CO.dat  77195  11/15/2013 04:00:08  731374590.96  3  4  731374590.96  76617  77195  578  3019  Y
H  YF_CO.dat  76444  11/17/2013 04:20:08  713606875.00  1  1  713606875.00  75822  76444  622  3310  Y

i wanted to sort the records along 4th and 5th column in ascending order of date and time using sort(or any other in that matter). I came a long way thru my code only at the end to realize that the way i am using sort ( sort -nk 5, i didn't try to sort the date but would be great if i cud take that into account too ) does not give me desired result. My constraint is that the solution must be as small as possible( great if i can use it to get input from a pipe and redirect to a text file) so that my already sizable code doesn't grow larger. I am using ksh shell in Solaris. I would appreciate any help in that matter.

Thanks

Try this

sort -k 4,5 your_file

or this (if you want to use a pipe)

... | sort -k 4,5
while read line
do 
echo -n "$line ";date -d "`echo $line | cut -d " " -f4,5`" '+%s'
done < filename | sort -n -k15 | awk 'NF=14'

Make it

sort -k4.7,4.11 -k4,5 file

to be safe when crossing year end.

1 Like

Data seems to contain leading blanks so sort should probably include -b:

sort -b -k4.7,4.10 -k4,5
2 Likes

-b ignores leading blanks, but, because of -k4,5 , that sort is needlessly sensitive to the number of blanks between fields 4 and 5.

A more flexible approach:

sort -b -k4.7,4 -k4,4.5 -k5

Regards,
Alister

1 Like