Linux中的sort排序

来源:互联网 发布:ubuntu怎么分区 编辑:程序博客网 时间:2024/04/30 20:13

默认排序

sort filename

[root@localhost ~]$ cat test.txt eeebbbdddaaaccc[root@localhost ~]$ sort test.txt aaabbbcccdddeee

去除重复行

sort -u filename

[root@localhost ~]$ cat test.txt eeebbbddddddaaaccc[root@localhost ~]$ sort -u test.txt aaabbbcccdddeee

按列排序

sort -k 2 filename 按第二列排序

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

指定分隔符

sort -t : filename

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

逆序排列

sort -r filename

[root@localhost ~]$ cat test.txt eeebbbdddaaaccc[root@localhost ~]$ sort -r test.txt eeedddcccbbbaaa

按数字排序

sort -n filename

[root@localhost ~]$ cat test3.txt 1021134[root@localhost ~]$ sort test3.txt 1011234[root@localhost ~]$ sort -n test3.txt 2101134

参考

  • Linux sort命令
  • 《sort帮你排序》-linux命令五分钟系列之二十六
  • GNU sort - What is the default algorithm used for comparison?
0 0
原创粉丝点击