uniq命令
uniq
uniq命令可以去除文件中的重复行,所有的重复行必须是相邻的,uniq经常和sort合用,进过sort排序,去除重复行效果更好
$uniq -d file 只输出file中的重复行,且只输出一次,但不输出唯一的行
$uniq -u file 只输出file中的唯一行(当然是一次啦)
$uniq -c file 在每行前显示重复次数,可与其他选项结合,例如-cu或-cd或-c
$uniq -i file 比较时忽略大小写
[root@localhost ~]# netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 localhost:6379 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:EtherNet/IP-1 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:sunrpc 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:http 0.0.0.0:* LISTEN
tcp 0 0 localhost.locald:domain 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:ipp 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 0 localhos:x11-ssh-offset 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:irdmi 0.0.0.0:* LISTEN
tcp 0 0 localhost:27017 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN
tcp 0 52 localhost.localdoma:ssh 192.168.56.101:57923 ESTABLISHED
tcp6 0 0 [::]:sunrpc [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:ipp [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
tcp6 0 0 localhos:x11-ssh-offset [::]:* LISTEN
去掉前两行没用的数据
[root@localhost ~]# netstat -at|sed -n '3,$p'
tcp 0 0 localhost:6379 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:EtherNet/IP-1 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:sunrpc 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:http 0.0.0.0:* LISTEN
tcp 0 0 localhost.locald:domain 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:ssh 0.0.0.0:* LISTEN
tcp 0 0 localhost:ipp 0.0.0.0:* LISTEN
tcp 0 0 localhost:smtp 0.0.0.0:* LISTEN
tcp 0 0 localhos:x11-ssh-offset 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:irdmi 0.0.0.0:* LISTEN
tcp 0 0 localhost:27017 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:mysql 0.0.0.0:* LISTEN
tcp 0 52 localhost.localdoma:ssh 192.168.56.101:57923 ESTABLISHED
tcp6 0 0 [::]:sunrpc [::]:* LISTEN
tcp6 0 0 [::]:ssh [::]:* LISTEN
tcp6 0 0 localhost:ipp [::]:* LISTEN
tcp6 0 0 localhost:smtp [::]:* LISTEN
tcp6 0 0 localhos:x11-ssh-offset [::]:* LISTEN
以空格为分隔符,取第六个字段
[root@localhost ~]# netstat -at|sed -n '3,$p'|awk -F " " '{print $6}'
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
ESTABLISHED
TIME_WAIT
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
将取到的结果排序
[root@localhost ~]# netstat -at|sed -n '3,$p'|awk -F " " '{print $6}'|sort -d
ESTABLISHED
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
LISTEN
排序后去重计数
[root@localhost ~]# netstat -at|sed -n '3,$p'|awk -F " " '{print $6}'|sort -d|uniq -c
1 ESTABLISHED
17 LISTEN
1 TIME_WAIT
[root@localhost ~]#