bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Improper behavior in sort utility


From: Philip Rowlands
Subject: Re: Improper behavior in sort utility
Date: Thu, 1 Dec 2005 04:08:33 +0000 (GMT)

On Wed, 30 Nov 2005, Mark Frost wrote:

It appears that sort does not examine the proper fields specified by
'-k' unless the '-n' option is given.

It would be helpful to know which sort locale is in use here; I'll assume it's en_US. Sort keys can be tricky, but I don't think any of the examples are bugs...

localhost$ sort -s -k 3 sort_challenge6
1 0 0 1 3
0 1 0 2 1
0 0 0 3 5
1 1 0 4 7
1 1 1 1 2
1 1 1 2 6
0 0 1 2 9
0 1 1 3 4
1 0 1 4 8
This sort appears to sort by all fields following field 3

Yes, this is the intended behaviour. Use "-k 3,3" to sort only on field 3.

localhost$ sort -s -k 3 -k 5 sort_challenge6
1 0 0 1 3
0 1 0 2 1
0 0 0 3 5
1 1 0 4 7
1 1 1 1 2
1 1 1 2 6
0 0 1 2 9
0 1 1 3 4
1 0 1 4 8
The second key argument is ignored and results are identical
to 'sort -s -k 3 sort_challenge6' above

This is equivalent to "-k 3,5 -k 5,5", therefore the keys overlap; the second argument is not ignored.

localhost$ sort -n -s -k 3 -k 5 sort_challenge6
0 1 0 2 1
1 0 0 1 3
0 0 0 3 5
1 1 0 4 7
1 1 1 1 2
0 1 1 3 4
1 1 1 2 6
1 0 1 4 8
0 0 1 2 9
Adding the '-n' corrects this as well.

I think (don't quote me on this) that using -n is equivalent to adding an "n" suffix to each sort key, i.e. "-k 3,5n -k 5,5n". What's happening here is that the whitespace terminates the third field number, allowing the fifth field tie-break to kick in.


Cheers,
Phil




reply via email to

[Prev in Thread] Current Thread [Next in Thread]