bash 同时按字典序和数字排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13816336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:59:43  来源:igfitidea点击:

sort lexicographically and numerically at the same time

bashsorting

提问by vkontori

I have a file tmp.txt (actually the output of a bash command) with entries like:

我有一个文件 tmp.txt(实际上是 bash 命令的输出),其中包含以下条目:

    ...
ammp            0                1.03683
ammp            10               2.69954
ammp            1                1.05712
ammp            11               2.70339
ammp            12               2.70339
ammp            2                1.88586
ammp            3                2.50103
ammp            4                2.64734
ammp            5                2.67462
ammp            6                2.68097
ammp            7                2.68631
ammp            8                 2.6904
ammp            9                2.69517
applu           0               0.678798
applu           10              0.922213
applu           1               0.901234
applu           11              0.923596
applu           12              0.923596
applu           2               0.901657
applu           3               0.903176
applu           4               0.908912
applu           5               0.913879
applu           6               0.914885
applu           7               0.915516
applu           8               0.917368
applu           9               0.920753
apsi            0                1.09037
apsi            10               2.20494
apsi            1                1.16651
apsi            11               2.24381
apsi            12               2.24381
apsi            2                1.49532
apsi            3                1.79137
apsi            4                1.79581
apsi            5                1.79601
apsi            6                1.80062
apsi            7                1.91269
apsi            8                 1.9579
apsi            9                2.00872
    ...

I want to sort it according to the first field and then the second. The problem is that I want lexicographical sort for the first field and numerical for the second.

我想根据第一个字段然后第二个字段对其进行排序。问题是我想要第一个字段的字典排序和第二个字段的数字排序。

I have tried the following:

我尝试了以下方法:

cat tmp.txt | sort -k1,2

sorts both fields lexicographically

按字典顺序对两个字段进行排序

cat tmp.txt | sort -k1 | sort -n -k2

the second sort messes the first.

第二种混淆了第一种。

cat tmp.txt | sort -s -k1.2n

supposedly stable sort that does the second field numerically. Does not work, and sorts second field lexicographically...

所谓的稳定排序,在数字上执行第二个字段。不起作用,并按字典顺序对第二个字段进行排序...

Ideas??

想法??

回答by dogbane

Try:

尝试:

sort -k 1,1 -k 2,2n file

This will sort on the first field alphabetically and then on the second field numerically.

这将按字母顺序对第一个字段进行排序,然后按数字顺序对第二个字段进行排序。

The sortmanpage has some examples.

sort男子页有一些例子。