1754946 Members
2701 Online
108827 Solutions
New Discussion юеВ

Sort Problem

 
SOLVED
Go to solution
C Kishore
Occasional Contributor

Sort Problem

We are sorting the file a.lst using the
following command (Sorting on 2 and 3 rd columns only)
sort -t':' -k2,3 a.lst

cat a.lst

1:3:3:4
1:4:3:5
1:2:3:4
1:5:3:4
1:4:3:4

The output we are getting is
1:2:3:4
1:3:3:4
1:4:3:4
1:4:3:5
1:5:3:4

The output we are expecting is :
1:2:3:4
1:3:3:4
1:4:3:5
1:4:3:4
1:5:3:4

Can anyone suggest us a solution.

Thanks
6 REPLIES 6
Michael Schulte zur Sur
Honored Contributor

Re: Sort Problem

Hi,

can you explain, why you want to sort column 4 in reverse order?

greetings,

Michael
C Kishore
Occasional Contributor

Re: Sort Problem

We are not sorting column 4 in reverse order,
our intention is to sort on cloumns 2 and 3 only. In the above example 1:4:3:5 preceeds
1:4:3:4, so we are expecting the same order.
melvyn burnard
Honored Contributor

Re: Sort Problem

Have you tried adding in the -n option for numeric sorting, rather than the standard lexicographical sort order?
My house is the bank's, my money the wife's, But my opinions belong to me, not HP!
Jean-Louis Phelix
Honored Contributor
Solution

Re: Sort Problem

Hi,

I often use nl to play this kind of games ...

nl -s: a.lst | sort -b -t':' -k3,4n -k1,1n | cut -f2- -d:

In this case it add temporarily a new sort field to keep trace of the original order which is not garanted on non sorted fields.

Regards.
It works for me (┬й Bill McNAMARA ...)
ranthony2cfl.rr.com
Regular Advisor

Re: Sort Problem

Most SORT programs will produce that same result. Column 2 contains a colon, for all records. If you sorted a.lst on column 2 only you should find it is already in sort-order, but the sequence of records would be unchanged.

The output you are getting is in sort-order for a sort on columns 2 and 3 -- you told the sort to not look at the columns after column 3, so for this sort, 1:4:3:4 and 1:4:3:5 are identical. The 1:4:3:5 preceeds in the output because it was encountered first in the input, and when the sort encountered an identical record (":4") you had defined no reason for it to change their order. To produce the output you desire, sort on columns 1 through 7 -- ie: the full length of the strings being sorted.
vitam largo et Deus omnipotens
Michael Schulte zur Sur
Honored Contributor

Re: Sort Problem

Hi,

Jean-Louis has the perfect answer. I was thinking to programme something like this, but seeing, that he was there before, I did not try to post a slightly different version. Take it, it works!!

greetings,

Michael