열 정렬을 유지하면서 셸의 테이블 출력에서 ​​불필요한 공백을 제거하려면 어떻게 해야 합니까?

열 정렬을 유지하면서 셸의 테이블 출력에서 ​​불필요한 공백을 제거하려면 어떻게 해야 합니까?

아래 명령은 공백으로 구분된 텍스트 테이블을 출력합니다. 열 정렬을 유지하면서 여기에서 불필요한 공백을 제거할 수 있는 도구가 있습니까?

$ 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 [::]:*

관련 정보