Subject: Re: bug#9995: problem about sort -u -k
From: Eric Blake <[email-masked]>
To: 夏凯 <[email-masked]>
Cc: [email-masked]
Date: Tue, 08 Nov 2011 11:54:17 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

tag 9995 notabug
thanks

On 11/08/2011 07:49 AM, 夏凯 wrote:
> when i use sort command with -k and -n together, i got the wrong result:

Thanks for the report; however, this is most likely not a bug in sort, 
but in your usage patterns.  Your sentence mentioned -k and -n together, 
but your example and subject line mentioned -u and -k together; so I'll 
assume that you got surprised by -u, not -n.

> 22:41:21#tp#~>  LC_ALL=C

Unless you also did 'export LC_ALL' at some point, this does not 
guarantee that child processes will see this setting in their environment.

> 22:41:39#tp#~>  /usr/local/bin/sort -u -k1,3 a
> 1 a q
> 1 a w
> 3 a w
> 22:41:48#tp#~>  /usr/local/bin/sort -u -k3 a
> 1 a q
> 1 a w
> 22:41:49#tp#~>  cat a
> 1 a q
> 1 a w
> 3 a w
> 22:41:52#tp#~>  /usr/local/bin/sort --version
> sort (GNU coreutils) 8.14

That's new enough that you can use the --debug option to see what was 
really going on:

$ LC_ALL=C ../coreutils/src/sort --debug -u -k1,3 a
sort: using simple byte comparison
1 a q
_____
1 a w
_____
3 a w
_____

Here, you compared all three lines, which were all distinct.

$ LC_ALL=C ../coreutils/src/sort --debug -u -k3 a
sort: using simple byte comparison
1 a q
    __
1 a w
    __

Here, you told sort to only look at a key of field three onwards, and to 
uniquify the results (that is, don't display multiple lines if they had 
the same sort key).  Since two lines both have the string " w" as the 
-k3 key, sort -u picked one of those lines (namely "3 a w") to be 
discarded on output.  This behavior matches POSIX rules.

Since you didn't tell us what output you were hoping to get, I can't 
tell you the proper command line that would match your expected output. 
  Feel free to reply, even while this bug is closed, if you need more 
help in getting the output you want.  Also, if you can prove that sort 
is doing something wrong, then feel free to reopen this bug with more 
evidence of why it is a bug in sort, including --debug output to back up 
your claim (but be aware that more than 90% of "bug" reports against 
sort have been debunked as user error rather than an actual bug in sort).

-- 
Eric Blake   [email-masked]    +1-801-349-2682
Libvirt virtualization library http://libvirt.org