Linux中的sort排序

默認排序

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

參考

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章