GNU bug report logs - #6393
The meaning of --key of sort command?

Previous Next

Package: coreutils;

Reported by: Peng Yu <pengyu.ut <at> gmail.com>

Date: Thu, 10 Jun 2010 16:40:02 UTC

Severity: normal

Tags: notabug

Done: Pádraig Brady <P <at> draigBrady.com>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 6393 in the body.
You can then email your comments to 6393 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6393; Package coreutils. (Thu, 10 Jun 2010 16:40:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Peng Yu <pengyu.ut <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Thu, 10 Jun 2010 16:40:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Peng Yu <pengyu.ut <at> gmail.com>
To: bug-coreutils <at> gnu.org
Subject: The meaning of --key of sort command?
Date: Thu, 10 Jun 2010 11:39:11 -0500
Hi,

I don't think that I completely understand what key means. In the
following example, I thought that --key=2 should order the lines by
the 2nd letter in each line without reordering the lines with the same
2nd letter. But it turns out my understanding is not correct. For
example, "u a" was before "a a" sorting, but "u a" is after "a a"
after sorting.

According to the man page, I'd think the sorting is based on the 2nd
column only but the 1st column. Why the 1st column matters in this
case?

-k, --key=POS1[,POS2]
              start a key at POS1 (origin 1), end it at POS2 (default  end  of
              line)


$ cat input.txt
u a
c c
a a
e e
p a
m e
a a
l e
a a
$ sort --key=2 input.txt
a a
a a
a a
p a
u a
c c
e e
l e
m e


-- 
Regards,
Peng




Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6393; Package coreutils. (Thu, 10 Jun 2010 16:46:02 GMT) Full text and rfc822 format available.

Message #8 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Peng Yu <pengyu.ut <at> gmail.com>
To: bug-coreutils <at> gnu.org
Subject: Re: The meaning of --key of sort command?
Date: Thu, 10 Jun 2010 11:45:36 -0500
On Thu, Jun 10, 2010 at 11:39 AM, Peng Yu <pengyu.ut <at> gmail.com> wrote:
> Hi,
>
> I don't think that I completely understand what key means. In the
> following example, I thought that --key=2 should order the lines by
> the 2nd letter in each line without reordering the lines with the same
> 2nd letter. But it turns out my understanding is not correct. For
> example, "u a" was before "a a" sorting, but "u a" is after "a a"
> after sorting.
>
> According to the man page, I'd think the sorting is based on the 2nd
> column only but the 1st column. Why the 1st column matters in this
> case?
>
> -k, --key=POS1[,POS2]
>              start a key at POS1 (origin 1), end it at POS2 (default  end  of
>              line)
>
>
> $ cat input.txt
> u a
> c c
> a a
> e e
> p a
> m e
> a a
> l e
> a a
> $ sort --key=2 input.txt
> a a
> a a
> a a
> p a
> u a
> c c
> e e
> l e
> m e

I forget to mention the following command gives me the same thing
(sorting on the 2nd fieled then the 1st field). But according to info
sort, it  should not the case.

   Example:  To sort on the second field, use `--key=2,2' (`-k 2,2').
     See below for more examples.


$ sort --key=2,2 input.txt
a a
a a
a a
p a
u a
c c
e e
l e
m e



-- 
Regards,
Peng




Information forwarded to owner <at> debbugs.gnu.org, bug-coreutils <at> gnu.org:
bug#6393; Package coreutils. (Thu, 10 Jun 2010 17:03:01 GMT) Full text and rfc822 format available.

Message #11 received at 6393 <at> debbugs.gnu.org (full text, mbox):

From: Eric Blake <eblake <at> redhat.com>
To: Peng Yu <pengyu.ut <at> gmail.com>
Cc: 6393 <at> debbugs.gnu.org
Subject: Re: bug#6393: The meaning of --key of sort command?
Date: Thu, 10 Jun 2010 11:01:51 -0600
[Message part 1 (text/plain, inline)]
On 06/10/2010 10:39 AM, Peng Yu wrote:
> Hi,
> 
> I don't think that I completely understand what key means. In the
> following example, I thought that --key=2 should order the lines by
> the 2nd letter in each line without reordering the lines with the same
> 2nd letter. But it turns out my understanding is not correct. For
> example, "u a" was before "a a" sorting, but "u a" is after "a a"
> after sorting.

'--key=2' implies a primary sort key from the second field (not second
letter), through the end of the line, with a secondary sort key of the
entire line.  '--key=2,2' implies a primary sort key of just the second
field, with a secondary sort key of the entire line.  You want
'--key=2,2 --stable', which disables the secondary sort over the entire
line.

-- 
Eric Blake   eblake <at> redhat.com    +1-801-349-2682
Libvirt virtualization library http://libvirt.org

[signature.asc (application/pgp-signature, attachment)]

bug closed, send any further explanations to Peng Yu <pengyu.ut <at> gmail.com> Request was from Pádraig Brady <P <at> draigBrady.com> to control <at> debbugs.gnu.org. (Wed, 14 Jul 2010 16:03:01 GMT) Full text and rfc822 format available.

Added tag(s) notabug. Request was from Pádraig Brady <P <at> draigBrady.com> to control <at> debbugs.gnu.org. (Wed, 14 Jul 2010 16:03:01 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 12 Aug 2010 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 14 years and 310 days ago.

Previous Next


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