列の並べ替えを維持しながら、シェルのテーブル出力から不要なスペースを削除するにはどうすればよいですか?

列の並べ替えを維持しながら、シェルのテーブル出力から不要なスペースを削除するにはどうすればよいですか?

次のコマンドは、スペースで区切られたテキストテーブルを出力します。列の整列を維持しながら、ここから不要なスペースを削除するツールはありますか?

$ 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))

おすすめ記事