Linux中的sort排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u012314976/article/details/56670691

默认排序

sort filename

[root@localhost ~]$ cat test.txt 
eee
bbb
ddd
aaa
ccc
[root@localhost ~]$ sort test.txt 
aaa
bbb
ccc
ddd
eee

去除重复行

sort -u filename

[root@localhost ~]$ cat test.txt 
eee
bbb
ddd
ddd
aaa
ccc
[root@localhost ~]$ sort -u test.txt 
aaa
bbb
ccc
ddd
eee

按列排序

sort -k 2 filename 按第二列排序

[root@localhost ~]$ cat test2.txt 
eee 23 555
ddd 11 666
aaa 22 888
ccc 23 111
bbb 24 222
[root@localhost ~]$ sort test2.txt 
aaa 22 888
bbb 24 222
ccc 23 111
ddd 11 666
eee 23 555
[root@localhost ~]$ sort -k 2 test2.txt 
ddd 11 666
aaa 22 888
ccc 23 111
eee 23 555
bbb 24 222
[root@localhost ~]$ sort -k 3 test2.txt 
ccc 23 111
bbb 24 222
eee 23 555
ddd 11 666
aaa 22 888

指定分隔符

sort -t : filename

[root@localhost ~]$ cat test2.txt 
eee:23:555
ddd:11:666
aaa:22:888
ccc:23:111
bbb:24:222
[root@localhost ~]$ sort -t : -k 2 test2.txt 
ddd:11:666
aaa:22:888
ccc:23:111
eee:23:555
bbb:24:222

逆序排列

sort -r filename

[root@localhost ~]$ cat test.txt 
eee
bbb
ddd
aaa
ccc
[root@localhost ~]$ sort -r test.txt 
eee
ddd
ccc
bbb
aaa

按数字排序

sort -n filename

[root@localhost ~]$ cat test3.txt 
10
2
11
34
[root@localhost ~]$ sort test3.txt 
10
11
2
34
[root@localhost ~]$ sort -n test3.txt 
2
10
11
34

参考

猜你喜欢

转载自blog.csdn.net/u012314976/article/details/56670691