Sign in with
Sign up | Sign in
Your question

Linux sort ignoring zeros

Last response: in Linux/Free BSD
Share
Anonymous
July 2, 2010 4:16:51 PM

Hi Guys...

I`m using this command 'sort -rn >> /tmp/log.log'

I want the latest date at the top of the file...this is ok from the 10th onwards..
but when you throw in 01 to 09 it puts these dates out of order... anyone have any ideas ??

I`ve scoured the net :( 

Derwood


a b 5 Linux
July 2, 2010 4:36:13 PM

I'm not quite sure what your input file looks like. It certainly seems to work OK if the dates are the first item on each line and are in the format:

yyyymmdd

which is what you need for a sort to work. Could you give us an example?
Anonymous
July 3, 2010 12:12:30 PM

Hi..

well the lines in the log start with
30-06-10_blah blah blah
31-06-10_blah etc
01-07-10_blah blah <---- this is the culprit because this is put at the bottom of the log file, where as I want the latest to be always at the top. with sort -rn
Related resources
Can't find your answer ? Ask !
a b 5 Linux
July 3, 2010 5:55:51 PM

The problem is with the parser. It doesnt work the way you want it to, though it is working correctly! 01<30<31



The easier solution to this is to change your date format:
yy-mm-dd will fix it, or else you will need to write your own parser, or do a for loop or something else creative.
a b 5 Linux
July 3, 2010 6:01:40 PM

Sort isn't going to work in the manner you are using it with that input. 10710 is less than 310610, so it is sorting correctly. You need to be a bit more creative. What about:

sort -rn -t '-' -k 3 -k2 -k1

i.e. divide the number into 3 fields separated by "-" then reverse sort on the 3rd, 2nd and 1st fields (in that order).
!