[英]Reverse sort order of a multicolumn file in BASH
I've the following file: 我有以下文件:
1 2 3
1 4 5
1 6 7
2 3 5
5 2 1
and I want that the file be sorted for the second column but from the largest number (in this case 6) to the smallest. 我希望文件按第二列排序,但从最大数量(在本例中为6)到最小数量。 I've tried with
我试过了
sort +1 -2 file.dat
but it gives me the inversed order. 但它给了我颠倒的顺序。
The results should be: 结果应该是:
1 6 7
1 4 5
2 3 5
5 2 1
1 2 3
sort -nrk 2,2
does the trick. 诀窍。
n for numeric sorting, r for reverse order and k 2,2 for the second column. n表示数字排序,r表示反向顺序,k 2,2表示第二列。
Have you tried -r ? 你试过吗? From the man page :
从手册页 :
-r, --reverse
-r, - 反向
reverse the result of comparisons
As mention most version of sort
have the -r
option if yours doesn't try tac
: 提到大多数版本的
sort
都有-r
选项,如果你的没有尝试tac
:
$ sort -nk 2,2 file.dat | tac
1 6 7
1 4 5
2 3 5
5 2 1
1 2 3
$ sort -nrk 2,2 file.dat
1 6 7
1 4 5
2 3 5
5 2 1
1 2 3
tac - concatenate and print files in reverse
tac - 反向连接和打印文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.