使用Linux sort命令对多个键进行排序 [英] sorting with multiple keys with Linux sort command

查看:125
本文介绍了使用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.

推荐答案

我在

在第二个字段上按数字排序,并通过排序解决关系在字段5的第三个和第四个字符上按字母顺序排列.用‘:’作为字段分隔符.

Sort numerically on the second field and resolve ties by sorting alphabetically on the third and fourth characters of field five. Use ‘:’ as the field delimiter.

      sort -t : -k 2,2n -k 5.3,5.4

请注意,如果您编写的是-k 2n而不是-k 2,2n,则排序使用了从第二个字段开始并扩展到行的结尾作为主数字键.对于绝大多数应用程序,将跨越多个字段的键视为数字不会达到您的期望.

Note that if you had written -k 2n instead of -k 2,2n sort would have used all characters beginning in the second field and extending to the end of the line as the primary numeric key. For the large majority of applications, treating keys spanning more than one field as numeric will not do what you expect.

我不确定将"1001 3"作为数字键来计算时会得到什么结果,但是将不会按照您的期望"是正确的.显然,正确的做法是独立指定每个键.

I'm not sure what it ends up with when it evaluates '1001 3' as a numeric key, but "will not do what you expect" is accurate. It seems clear that the Right Thing to do is to specify each key independently.

同一网页上的内容是关于解决联系"的.

The same web page says this about resolving "ties".

最后,当所有键都相等时,最后一种排序方法是比较整行就像--reverse(-r)之外没有其他排序选项指定的.

Finally, as a last resort when all keys compare equal, sort compares entire lines as if no ordering options other than --reverse (-r) were specified.

我将承认我对如何解释它有些迷惑.

I'll confess I'm a little mystified about how to interpret that.

这篇关于使用Linux sort命令对多个键进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆