Shell 编程 排序工具 sort 和 uniq

匿名 (未验证) 提交于 2019-12-02 21:59:42

本篇主要写一些shell脚本排序工具的使用。


sort

sort是一个以行为单位对文件内容进行排序的工具,也可以根据不同的数据类型来排序。

  • sort [选项] 参数

-f:忽略大小写
-b:忽略每行前面的空格
-M:按照月份进行排序
-n:按照数字进行排序
-r:反向排序
-u:等同于uniq,表示相同的数据仅显示一行
-t:指定分隔符,默认使用Tab键分隔
-o <输出文件>:将排序后的结果转存至指定文件
-k:指定排序区域

  • /etc/passwd文件中的账号进行排序
[root@localhost ~]# sort /etc/passwd adm:x:3:4:adm:/var/adm:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin halt:x:7:0:halt:/sbin:/sbin/halt lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin root:x:0:0:root:/root:/bin/bash shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
  • /etc/passwd文件中第3列进行反向排序
[root@localhost ~]# sort -t ':' -rk 3 /etc/passwd nobody:x:99:99:Nobody:/:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin halt:x:7:0:halt:/sbin:/sbin/halt shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown sync:x:5:0:sync:/sbin:/bin/sync lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin root:x:0:0:root:/root:/bin/bash
  • /etc/passwd文件中第3列进行排序,并将输出内容保存至user.txt
[root@localhost ~]# sort -t ':' -k 3 /etc/passwd -o user.txt [root@localhost ~]# cat user.txt  root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin bin:x:1:1:bin:/bin:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin mail:x:8:12:mail:/var/spool/mail:/sbin/nologin dbus:x:81:81:System message bus:/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin chrony:x:998:996::/var/lib/chrony:/sbin/nologin polkitd:x:999:997:User for polkitd:/:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin

uniq

uniq工具通常与sort命令结合使用,用于报告或者忽略文件中的重复行。

  • uniq [选项] 参数

-c:进行计数
-d:仅显示重复行
-u:仅显示出现一次的行

  • 删除test.txt文件中重复行
[root@localhost ~]# cat test.txt  centos5 centos5 centos5 centos6 centos5 centos5 centos7 centos8 centos8 centos8
[root@localhost ~]# uniq test.txt  centos5 centos6 centos5 centos7 centos8
  • 删除test.txt文件中重复行,并统计改行重复次数
[root@localhost ~]# uniq -c test.txt       3 centos5       1 centos6       2 centos5       1 centos7       3 centos8
  • 查找test.txt文件中重复行
[root@localhost ~]# uniq -d test.txt centos5 centos5 centos8
  • 查找test.txt文件中只出现一次的行
[root@localhost ~]# uniq -u test.txt centos6 centos7
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!