一种更有效的方式来对 netstat 命令的结果进行排序

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因为当我试图覆盖它时它不起作用。

Ser*_*nyy 5

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 个字段。