bug#6393: The meaning of --key of sort command?

2010-06-10 Thread Peng Yu
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





bug#6393: The meaning of --key of sort command?

2010-06-10 Thread Peng Yu
On Thu, Jun 10, 2010 at 11:39 AM, Peng Yu pengyu...@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





bug#6393: The meaning of --key of sort command?

2010-06-10 Thread Eric Blake
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   ebl...@redhat.com+1-801-349-2682
Libvirt virtualization library http://libvirt.org



signature.asc
Description: OpenPGP digital signature