当数字相等时按数字排序,但不按字母顺序排序 [英] Sorting numerically but not alphabetically when numbers are equal
本文介绍了当数字相等时按数字排序,但不按字母顺序排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的文件:
I have a file like this:
A 0.77
C 0.98
B 0.77
Z 0.77
G 0.65
我想按降序对文件进行数字排序.我使用了以下代码:
I want to sort the file numerically in descending order. I used this code:
sort -gr -k2,2 file.txt
我得到这个:
C 0.98
Z 0.77
B 0.77
A 0.77
G 0.65
在我的真实文件中,我有几列具有相同的编号,并且它们按字母顺序排列.我想要的是在数字相等时以数字方式而不是字母顺序排序,我想获得那些未按字母顺序排序的列:
In my real file I have several columns with the same number and they are ordered alphabetically. What I want is to sort numerically but not alphabetically when the numbers are equal, I want to obtain those columns unsorted alphabetically:
C 0.98
B 0.77
Z 0.77
A 0.77
G 0.65
但是任何随机顺序都可以.
But any random order is fine.
推荐答案
您可以使用以下sort
:
sort -k2rn -k1R file
C 0.98
B 0.77
Z 0.77
A 0.77
G 0.65
使用了2个排序选项:
-
-k2rn
:第一个排序键是第2列;数字,反向 -
-k1R
:第二个排序键是第1列; 随机
-k2rn
: First sort key is column 2; numerical, reverse-k1R
: Second sort key is column 1; random
这篇关于当数字相等时按数字排序,但不按字母顺序排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文