我想仅打印选定的lsof
输出列
$1,$2,$3,$5,$8,$10
,并在打印所有初始输出列时使用uniq -c
该列。PID
我在 macOS 上。我正在使用awk
, sort
,uniq -c
我将我的机器的 IP 地址存储在一个变量中:$ETH_IP
ETH_IP="192.168.10.191" && echo $ETH_IP
192.168.10.191
这是我当前的命令,带有一些格式及其结果:
(lsof -alP -Ts -i @"$ETH_IP" -s TCP:ESTABLISHED,LISTEN,CLOSE_WAIT +c0 |sed 's|\\x20|_|g' |awk '{print $1,$2,$3,$5,$8,$10}' |column -t)
COMMAND PID USER TYPE NODE
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
Mail 668 501 IPv4 TCP (ESTABLISHED)
GitHub_Desktop_Helper 1196 501 IPv4 TCP (ESTABLISHED)
GitHub_Desktop_Helper 1196 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
Dropbox 74706 501 IPv4 TCP (ESTABLISHED)
Dropbox 74706 501 IPv4 TCP (ESTABLISHED)
Dropbox 74706 501 IPv4 TCP (ESTABLISHED)
DropboxFileProvider 74707 501 IPv4 TCP (ESTABLISHED)
DropboxFileProvider 74707 501 IPv4 TCP (ESTABLISHED)
DropboxFileProvider 74707 501 IPv4 TCP (ESTABLISHED)
我想使用类似 的内容| sort | uniq -c | sort -n
。
使用第 2 列 (PID) 中的值uniq
。
使用awk
并保留如上所示的多列格式。
因此,每个进程只有一个列表,行前面列出了原始行的数量。
我正在寻找的模型。
COMMAND PID USER TYPE NODE
18 Mail 668 501 IPv4 TCP (ESTABLISHED)
2 GitHub_Desktop_Helper 1196 501 IPv4 TCP (ESTABLISHED)
2 Code_Helper 1493 501 IPv4 TCP (ESTABLISHED)
14 com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
3 Dropbox 74706 501 IPv4 TCP (ESTABLISHED)
2 DropboxFileProvider 74707 501 IPv4 TCP (ESTABLISHED)
我面临的问题是 HEADER 行也被计算在内uniq -c
(lsof -alP -Ts -i @"$ETH_IP" -s TCP:ESTABLISHED,LISTEN,CLOSE_WAIT +c0 |sed 's|\\x20|_|g' |awk '{print $1,$2,$3,$5,$8,$10}' |column -t) | sort -k 2,2 | uniq -c | sort -n -k 3,3
1 COMMAND PID USER TYPE NODE
3 accountsd 667 501 IPv4 TCP (ESTABLISHED)
16 Mail 668 501 IPv4 TCP (ESTABLISHED)
2 GitHub_Desktop_Helper 1196 501 IPv4 TCP (ESTABLISHED)
12 com.apple.WebKit.Networking 12635 501 IPv4 TCP (ESTABLISHED)
2 Dropbox 74706 501 IPv4 TCP (ESTABLISHED)
2 DropboxFileProvider 74707 501 IPv4 TCP (ESTABLISHED)
这个想法来自于这个命令及其输出。
lsof -alP -Ts -i @"$ETH_IP" -s TCP:ESTABLISHED,LISTEN,CLOSE_WAIT +c0 | awk 'NR>2{print $1}' | sort | uniq -c | sort -n
1 CloudTelemetryService
1 cloudd
2 Dropbox
2 DropboxFileProvider
2 GitHub\x20Desktop\x20Helper
14 com.apple.WebKit.Networking
17 Mail
这种方法虽然能得到我想要的结果,但不太好用。如果有更好/更快的解决方案,我会很感兴趣。