使用Linux sort命令对多个键进行排序 [英] sorting with multiple keys with Linux sort command
本文介绍了使用Linux sort命令对多个键进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有这个文件.
$ 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
但是,我认为 sort -k2n a.txt
应该也可以,但不能.
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
似乎它按第二列,然后按第一列而不是第三列排序.为什么会这样呢?是不是错误?原因 sort -k2 a.txt
可以很好地处理上述数据,因为这些数字只是固定宽度.
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.
我的排序版本是cygwin中的 sort(GNU coreutils)8.15
.
My sort version is sort (GNU coreutils) 8.15
in cygwin.
推荐答案
查看全文