GNU bug report logs - #9995
problem about sort -u -k

Previous Next

Package: coreutils;

Reported by: 夏凯 <walkerxk <at> gmail.com>

Date: Tue, 8 Nov 2011 17:25:15 UTC

Severity: normal

Tags: notabug

Done: Eric Blake <eblake <at> redhat.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: Eric Blake <eblake <at> redhat.com>
Cc: tracker <at> debbugs.gnu.org
Subject: bug#9995: closed (problem about sort -u -k)
Date: Tue, 08 Nov 2011 18:55:04 +0000
[Message part 1 (text/plain, inline)]
Your message dated Tue, 08 Nov 2011 11:54:17 -0700
with message-id <4EB97AD9.4050502 <at> redhat.com>
and subject line Re: bug#9995: problem about sort -u -k
has caused the debbugs.gnu.org bug report #9995,
regarding problem about sort -u -k
to be marked as done.

(If you believe you have received this mail in error, please contact
help-debbugs <at> gnu.org.)


-- 
9995: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=9995
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
From: 夏凯 <walkerxk <at> gmail.com>
To: bug-coreutils <at> gnu.org
Subject: problem about sort -u -k
Date: Tue, 8 Nov 2011 22:49:12 +0800
when i use sort command with -k and -n together, i got the wrong result:
22:41:21#tp#~> LC_ALL=C
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
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and Paul Eggert.
22:41:57#tp#~>
why is that?
i read http://www.gnu.org/s/coreutils/manual/html_node/sort-invocation.html,
but got nothing about this.
any help is appreciate.
-- 
contact me:
MSN: walkerxk <at> gmail.com
GTALK: walkerxk <at> gmail.com


[Message part 3 (message/rfc822, inline)]
From: Eric Blake <eblake <at> redhat.com>
To: 夏凯 <walkerxk <at> gmail.com>
Cc: 9995-done <at> debbugs.gnu.org
Subject: Re: bug#9995: problem about sort -u -k
Date: Tue, 08 Nov 2011 11:54:17 -0700
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   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org


This bug report was last modified 13 years and 255 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.