Fut*_*get 1 command-line text-processing netstat
我试图只对与 firefox 无关的 IP 地址进行排序,并将它们写入一个文件中。我结束了这个命令行,完成了这项工作,但我想知道是否有办法做得更好。
netstat -antp | grep -E ?:80 | grep -v firefox > ipadress.txt && awk '{print $5}' ipadress.txt > ipadress1.txt
Run Code Online (Sandbox Code Playgroud)
有没有办法通过删除 :80 端口来获得更干净的输出?我使用了ipadress1.txt
因为当我试图覆盖它时它不起作用。
让awk
做的所有工作:
$ netstat -antp | awk '/:80/ && !/\/firefox/{print $5}'
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
151.101.129.69:80
67.132.183.24:80
91.189.89.144:80
151.101.129.69:80
:::*
Run Code Online (Sandbox Code Playgroud)
典型的awk
程序是这样构建的:
/pattern/ {code to run if pattern matched}
Run Code Online (Sandbox Code Playgroud)
在这种特殊情况下,我们使用两种模式:我们寻找其中没有的:80
字符串和行/firefox
。该&&
是逻辑与装置,该装置符合双方在左边和右边的图案。如果我们有这样的行匹配 - 执行代码块,它只打印第 5 个字段。