下面的命令输出一个以空格分隔的文本表,是否有工具可以删除此处不必要的空格,同时仍保持列对齐?
$ 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 :::*
答案1
事实证明,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))
答案2
$ ss -ltpn | awk '{ gsub("\\s{2,}"," "); print; }'
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 100 127.0.0.1:25 0.0.0.0:*
LISTEN 0 50 *:1716 *:* users:(("kdeconnectd",pid=2952,fd=15))
LISTEN 0 50 *:1717 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
LISTEN 0 100 [::1]:25 [::]:*