1

Below command outputs a table of space delimited text, is there a tool to remove the unnecessary spacing here while still keeping the columns aligned?

$ 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                                                                                                  :::*       
2
  • 3
    How do you define "unnecessary"? Do you still need the columns to be aligned, or is it enough to just replace all multiple spaces with a tab character so that you can parse it easily even if it doesn't look great when printed? – terdon May 7 '20 at 16:58
  • Thanks, yes, still keeping columns aligned, updating question now. – Elijah Lynn May 7 '20 at 17:15
3

Turns out that ss can do this for you if you just pipe the output or redirect it to a file. For example, on my system, without piping, I get this:

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

If I simply pipe to cat, however, I get:

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

I also get the same output if I just redirect to a file: sudo ss -ltpn > file.


For a more general solution, you can use column. For example, given this input file:

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

I can pass it through column -t to pretty print it:

$ 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))
8
  • Thanks, I just tried your suggesting on piping to cat and also a file and the output was mangled a bit, spacing wasn't correctly keeping the columns aligned. But then tried on my local machine and it works great. So something is up with the different environments. Marked as correct answer though, thanks! – Elijah Lynn May 7 '20 at 17:10
  • @ElijahLynn the way it is printed depends on the size of your terminal (or, I am assuming, the value of the $COLUMNS variable if you're not using a GUI terminal emulator). I suspect that if you just resize your terminal, you'll see better results. Alternatively, try the column solution from the updated answer. – terdon May 7 '20 at 17:13
  • Ahh, my local has a newer version of ss that just uses the right amount of spaces so maybe that was a bug/improvement in ss that was fixed. Both shells were the same size. Using your suggestion, this variation works great ss -lptn | column -t (just piping to column -t. – Elijah Lynn May 7 '20 at 17:18
  • @ElijahLynn that one, at least on my system, breaks the headers. The Local Address:Port column becomes Local and the Peer is empty. That's why I used the N option to give the names of the columns and tail -n+2 to pass everything except the first line to column. Are you sure that the simple ss -lptn | column -t works for you and doesn't mangle the headers? – terdon May 7 '20 at 17:22
  • Interesting, just tested on my local too (latest everything, Arch) and works nicely. I just updated my question here with the output from the server (same effect as local) > serverfault.com/questions/1016031/… – Elijah Lynn May 7 '20 at 17:26
0
$ 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 [::]:*
1
  • 1
    That's a convoluted and non-portable way to write: ss -ltpn | tr -s '[:space:]' – Stéphane Chazelas May 7 '20 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.