Shell
如何從 shell 中的表輸出中刪除不必要的空格,同時保持列對齊?
下面的命令輸出一個以空格分隔的文本表,是否有一種工具可以刪除此處不必要的間距,同時仍保持列對齊?
$ sudo ss -ltpn State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 32 10.218.108.1:53 *:* users:(("dnsmasq",pid=10242,fd=9)) LISTEN 0 128 *:22 *:* users:(("sshd",pid=1111,fd=3)) LISTEN 0 32 fd42:9324:ab98:50fb::1:53 :::* users:(("dnsmasq",pid=10242,fd=13)) LISTEN 0 32 fe80::c024:c5ff:fe68:999e%lxdbr0:53 :::* users:(("dnsmasq",pid=10242,fd=11)) LISTEN 0 128 :::22 :::*
事實證明,
ss
如果您只是通過管道輸出或將其重定向到文件,那麼它可以為您做到這一點。例如,在我的系統上,沒有管道,我得到這個:$ sudo ss -ltpn State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:53939 0.0.0.0:* users:(("spotify",pid=4152748,fd=115)) LISTEN 0 10 0.0.0.0:57621 0.0.0.0:* users:(("spotify",pid=4152748,fd=96)) LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=822,fd=3)) LISTEN 0 128 127.0.0.1:10391 0.0.0.0:* users:(("Enpass",pid=2193055,fd=38)) LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=818,fd=8)) LISTEN 0 5 127.0.0.1:9292 0.0.0.0:* users:(("emacs",pid=178419,fd=13)) LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=314,fd=4),("systemd",pid=1,fd=106)) LISTEN 0 5 127.0.0.1:34512 0.0.0.0:* users:(("purevpnd",pid=839,fd=6)) LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=822,fd=4)) LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=818,fd=7)) LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=314,fd=6),("systemd",pid=1,fd=128))
但是,如果我只是通過管道傳輸到
cat
,我會得到:$ sudo ss -ltpn | cat State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess LISTEN 0 128 0.0.0.0:53939 0.0.0.0:* users:(("spotify",pid=4152748,fd=115)) LISTEN 0 10 0.0.0.0:57621 0.0.0.0:* users:(("spotify",pid=4152748,fd=96)) LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=822,fd=3)) LISTEN 0 128 127.0.0.1:10391 0.0.0.0:* users:(("Enpass",pid=2193055,fd=38)) LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=818,fd=8)) LISTEN 0 5 127.0.0.1:9292 0.0.0.0:* users:(("emacs",pid=178419,fd=13)) LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=314,fd=4),("systemd",pid=1,fd=106)) LISTEN 0 5 127.0.0.1:34512 0.0.0.0:* users:(("purevpnd",pid=839,fd=6)) LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=822,fd=4)) LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=818,fd=7)) LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=314,fd=6),("systemd",pid=1,fd=128))
如果我只是重定向到一個文件,我也會得到相同的輸出:
sudo ss -ltpn > file
.對於更通用的解決方案,您可以使用
column
. 例如,給定這個輸入文件:$ cat file State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:53939 0.0.0.0:* users:(("spotify",pid=4152748,fd=115)) LISTEN 0 10 0.0.0.0:57621 0.0.0.0:* users:(("spotify",pid=4152748,fd=96)) LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=822,fd=3)) LISTEN 0 128 127.0.0.1:10391 0.0.0.0:* users:(("Enpass",pid=2193055,fd=38)) LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=818,fd=8)) LISTEN 0 5 127.0.0.1:9292 0.0.0.0:* users:(("emacs",pid=178419,fd=13)) LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=314,fd=4),("systemd",pid=1,fd=106)) LISTEN 0 5 127.0.0.1:34512 0.0.0.0:* users:(("purevpnd",pid=839,fd=6)) LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=822,fd=4)) LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=818,fd=7)) LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=314,fd=6),("systemd",pid=1,fd=128))
我可以通過它
column -t
來漂亮地列印它:$ column -t -N"State,Recv-Q,Send-Q,Local Address:Port,Peer Address:Port,Process" <(tail -n +2 file) State Recv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 128 0.0.0.0:53939 0.0.0.0:* users:(("spotify",pid=4152748,fd=115)) LISTEN 0 10 0.0.0.0:57621 0.0.0.0:* users:(("spotify",pid=4152748,fd=96)) LISTEN 0 128 0.0.0.0:22 0.0.0.0:* users:(("sshd",pid=822,fd=3)) LISTEN 0 128 127.0.0.1:10391 0.0.0.0:* users:(("Enpass",pid=2193055,fd=38)) LISTEN 0 5 127.0.0.1:631 0.0.0.0:* users:(("cupsd",pid=818,fd=8)) LISTEN 0 5 127.0.0.1:9292 0.0.0.0:* users:(("emacs",pid=178419,fd=13)) LISTEN 0 4096 0.0.0.0:111 0.0.0.0:* users:(("rpcbind",pid=314,fd=4),("systemd",pid=1,fd=106)) LISTEN 0 5 127.0.0.1:34512 0.0.0.0:* users:(("purevpnd",pid=839,fd=6)) LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=822,fd=4)) LISTEN 0 5 [::1]:631 [::]:* users:(("cupsd",pid=818,fd=7)) LISTEN 0 4096 [::]:111 [::]:* users:(("rpcbind",pid=314,fd=6),("systemd",pid=1,fd=128))