netstat -lntp|awk {'print $4'}|awk -F ':' '{if ($NF~/^[0-9]$/) print $NF}'|sort|uniq
netstat -lntp|awk '{print $4}'|awk -F ':' '/[0-9]/{print $NF}'|sort|uniq
netstat -lntp|awk {'print $4'}|awk -F ':' '{print $NF}'|egrep "^[0-9]$"|sort|uniq
结果:
22
3306
为什么执行这句无任何显示
netstat -lntp|awk {‘print $4’}|awk -F ‘:’ ‘/^[0-9]*$/{print $1}’|sort|uniq
但在txt文本又能生效
[root@oldboy scripts]# cat test.txt
(only
Local
22
a3306
22
3306
4473f
56789
[root@oldboy scripts]# cat test.txt|awk -F ':' '/[0-9]/{print $1}'|sort|uniq
22
3306
4473f
56789
a3306
[root@oldboy scripts]# cat test.txt|awk -F ':' '/^[0-9]$/{print $1}'|sort|uniq
[root@oldboy scripts]# cat test.txt|awk -F ':' '/^[0-9]*$/{print $1}'|sort|uniq
22
3306
56789
总结:
- netstat 不需要多次匹配
- cat 需要多次匹配
- netstat 是一行处理 cat 是多行处理