
當我在 Red Hat Linux 上使用這個指令時
/usr/sbin/ss -i
我得到以下輸出:
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 <IP_ADD:PORT> <IP_ADD:PORT>
ts sack wscale:2,2 rto:204 rtt:4.5/6.5 ato:40 cwnd:3
ESTAB 0 0 <IP_ADD:PORT> <IP_ADD:PORT>
ts sack wscale:2,2 rto:213 rtt:13.875/18.5 ato:40
ESTAB 0 0 <IP_ADD:PORT> <IP_ADD:PORT>
ts sack wscale:2,2 rto:201 rtt:1.875/0.75 ato:40
ESTAB 0 0 <IP_ADD:PORT> <IP_ADD:PORT>
ts sack wscale:9,2 rto:201 rtt:1.875/0.75 ato:40
每當我嘗試透過管道傳輸grep
到該命令時ex
:
/usr/sbin/ss -i | grep <SOME_IP_ADD>
我有這個輸出
ESTAB 0 0 <IP_ADD:PORT> <IP_ADD:PORT>
ESTAB 0 0 <IP_ADD:PORT> <IP_ADD:PORT>
ESTAB 0 0 <IP_ADD:PORT> <IP_ADD:PORT>
請注意 grep 不包含此內容:
ts sack wscale:2,2 rto:204 rtt:4.5/6.5 ato:40 cwnd:3
因為它在另一條線上。那麼,每當我使用此命令或其他 Linux 命令時,如何調整列寬呢?這樣輸出就不會自動換行或轉到下一行?他們有更好的方法嗎?
答案1
如果您的 grep 有它,請嘗試該-A1
選項。
看起來這不是換行的情況,而是條目位於單獨的行上。
/usr/sbin/ss -i | grep -A1 <SOME_IP_ADD>
看看Context Line Control
在man grep
.
另一個選擇是使用
-P Perl-regex
-z suppress-newline
-o print only matching
如:
ss -i | grep -Pzo '.*IPADDRESS.*\n.*'
那麼你就不會得到上下文給出的周圍的破折號。
另一個選擇是 sed:
sed -n '/IPADDRESS/{N;p}'
# Or joining the two lines by:
ss -i | sed -n '/IPADDRESS/N;s/\n/ /p'
awk:
awk '/IPADDRESS/{print; getline; print}'
# Or as joined lines:
awk '/IPADDRESS/{printf "%s ", $0; getline; print}'
答案2
該ts sack ...
行位於該ESTAB ...
行下方,因為這ss
通常是格式化此類資訊的方式。它沒有被換行。您可以grep
使用該-A1
標誌將這兩行包含在其中:
ss -i | grep <IP ADDRESS> -A1
您可以透過命令傳遞每個符合項目並將其合併到一行sed
:
ss -i | grep <IP ADDRESS> -A1 | sed '/^--$/d;N;s/\n/ /g'