Say I have this file.
$ cat a.txt
c 1002 4
f 1001 1
d 1003 1
a 1001 3
e 1004 2
b 1001 2
I want to sort it by the second column and then by the third column. Column two are numbers, while column 3 can be treated as string. I know the following command works well.
$ sort -k2,2n -k3,3 a.txt
f 1001 1
b 1001 2
a 1001 3
c 1002 4
d 1003 1
e 1004 2
However, I think sort -k2n a.txt should also work, while it does not.
$ sort -k2n a.txt
a 1001 3
b 1001 2
f 1001 1
c 1002 4
d 1003 1
e 1004 2
Seems like it sorts by column two, and then by column one instead of column three. Why is this happening? Is it a bug or not? Cause sort -k2 a.txt works ok with above data since those numbers are just fixed width.
My sort version is sort (GNU coreutils) 8.15 in cygwin.
sort -k2 a.txtwill work in this case.-k2tells it to sort using a key that starts at field 2 and continues to the end of line.-k2ntells it to sort field 2 in numeric order; that might mean the sort key ends on encountering whitespace between fields 2 and 3. It might be a good idea to paste the version of your sort into the question somewhere.sort (GNU coreutils) 8.5I am able to reproduce the described behaviour on Debian stable."1001 3"etc. as by-k2nare not numeric.sort -k2 -uandsort -k2n -uyield different results on your file. I eventually figured out why (a 1001 3andb 1001 2are both numerically identical to 1001, but not equal as strings), but, still, argh!