[英]Bash sorting comma delimited columns numerically and then alphabetically
I'm trying to sort 3 columns numerically in reverse (descending) by the third column first and then sort by the first column alphabetically (to break ties). 我试图通过第三列以反向(降序)数字排序3列,然后按字母顺序排序第一列(打破关系)。 Entries are delimited by commas( ,
). 条目以逗号( ,
)分隔。
For example, my dataset is: 例如,我的数据集是:
y,5,50
x,10,50
z,4,100
Expected output: 预期产量:
z,4,100
x,10,50
y,5,50
However the output I am getting is: 但是我得到的输出是:
z,4,100
y,5,50
x,10,50
I am using: 我在用:
sort -t, -k3,3 -n -r -k1,1 filename
Not sure why this is not working. 不知道为什么这不起作用。
I suggest to replace -k3,3 -n -r
by -k3,3nr
: 我建议更换-k3,3 -n -r
通过-k3,3nr
:
sort -t, -k3,3nr -k1,1 file
Output: 输出:
z,4,100 x,10,50 y,5,50
The reason what you're suggesting doesn't work is becuase you've applied the flags -n
, -r
globally hence the alphabetical sorting is also -r
eversed. 你所建议不起作用的原因是监守你应用的标志-n
, -r
全球因此字母排序也-r
eversed。 To apply the flag on a per key basis, use: 要在每个密钥的基础上应用标志,请使用:
sort -t, -k3,3nr -k1,1 filename
This gives the expected output: 这给出了预期的输出:
z,4,100
x,10,50
y,5,50
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.