Operating System - Linux
1753531 Members
4966 Online
108795 Solutions
New Discussion юеВ

Sort command has incorrect results or I don't know what I'm doing

 
SOLVED
Go to solution
Kurt Redlitz
New Member

Sort command has incorrect results or I don't know what I'm doing

I need to sort the following (example) by
they second field(date) and then the third
field (time), using the comma as the field
separator.

1,20051018,154700,67
2,20051018,142000,8
3,20051020,1111,67
4,20051020,222,8

Using cat txt | sort -t , -k2n -k3n
does not produce the correct results, (but
does on other OS's). It comes out like this:
4,20051020,222,8
3,20051020,1111,67
2,20051018,142000,8
1,20051018,154700,67

(The correct results should have the first
fields as 2,1,4,3)

I cannot seem to find
any parameters to get this to sort properly.

HP-UX B.11.11 U 9000/800
6 REPLIES 6
OldSchool
Honored Contributor
Solution

Re: Sort command has incorrect results or I don't know what I'm doing

sort -t, -k2,2n -k3,3n

"cat" isn't necessary btw
Sandman!
Honored Contributor

Re: Sort command has incorrect results or I don't know what I'm doing

Works for me...what OS are you running this on?

# cat infile
1,20051018,154700,67
2,20051018,142000,8
3,20051020,1111,67
4,20051020,222,8

# sort -t, -k2,2n -k3,3n infile
2,20051018,142000,8
1,20051018,154700,67
4,20051020,222,8
3,20051020,1111,67
OldSchool
Honored Contributor

Re: Sort command has incorrect results or I don't know what I'm doing

check the man page regarding keydefs, if you don't specify the ending field, it sorts using startfield thru end of line. the -k2,2n restricts the first sort key to the second field only, the 3,3 does the third likewise
Kurt Redlitz
New Member

Re: Sort command has incorrect results or I don't know what I'm doing

Thanks so much, I missed the part about the using the end of the line if the field-end is not specified.

Dennis Handly
Acclaimed Contributor

Re: Sort command has incorrect results or I don't know what I'm doing

You haven't assigned any points yet. Please read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33

You should also close this thread indicating you have a solution.
Kurt Redlitz
New Member

Re: Sort command has incorrect results or I don't know what I'm doing

The solution was provided in the replies.