Sign in with
Sign up | Sign in

Linux sort ignoring zeros

Last response: in Linux/Free BSD
Share

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?

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

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.

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).
Ask the community
!