셸 명령의 출력 형식을 열로 지정하고 생성되는 열 수를 제한하려면 어떻게 해야 합니까?

셸 명령의 출력 형식을 열로 지정하고 생성되는 열 수를 제한하려면 어떻게 해야 합니까?

내가 달린다고 해 systemctl | grep running | column -t

다음과 같은 결과가 나타납니다.

init.scope                       loaded  active  running  System         and            Service     Manager
session-23.scope                 loaded  active  running  Session        23             of          user          admin
auditd.service                   loaded  active  running  Security       Auditing       Service
chronyd.service                  loaded  active  running  NTP            client/server
crond.service                    loaded  active  running  Command        Scheduler
dbus.service                     loaded  active  running  D-Bus          System         Message     Bus
[email protected]               loaded  active  running  Getty          on             tty1
gssproxy.service                 loaded  active  running  GSSAPI         Proxy          Daemon
irqbalance.service               loaded  active  running  irqbalance     daemon
named.service                    loaded  active  running  Berkeley       Internet       Name        Domain        (DNS)
NetworkManager.service           loaded  active  running  Network        Manager
nfs-idmapd.service               loaded  active  running  NFSv4          ID-name        mapping     service
nfs-mountd.service               loaded  active  running  NFS            Mount          Daemon
nfsdcld.service                  loaded  active  running  NFSv4          Client         Tracking    Daemon
oddjobd.service                  loaded  active  running  privileged     operations     for         unprivileged  applications
polkit.service                   loaded  active  running  Authorization  Manager
postfix.service                  loaded  active  running  Postfix        Mail           Transport   Agent
rpc-gssd.service                 loaded  active  running  RPC            security       service     for           NFS           client    and     server
rpc-statd.service                loaded  active  running  NFS            status         monitor     for           NFSv2/3       locking.
rpcbind.service                  loaded  active  running  RPC            Bind
rsyslog.service                  loaded  active  running  System         Logging        Service
[email protected]       loaded  active  running  Serial         Getty          on          ttyS1
smartd.service                   loaded  active  running  Self           Monitoring     and         Reporting     Technology    (SMART)   Daemon

유사한 출력을 유지하면서 "설명" 열의 형식을 지정하여 공간을 기준으로 더 많은 열로 분할되지 않도록 하려면 어떻게 해야 합니까? 예를 들어 원래 출력과 마찬가지로 읽기 쉬운 방식으로 다섯 번째 열의 형식을 지정하려면 어떻게 해야 합니까 systemctl?

나는 이 질문을 보고 매우 가까워졌습니다.https://stackoverflow.com/questions/6462894/how-can-i-format-the-output-of-a-bash-command-in-neat-columns

답변1

이것은 아마도 POSIX awk를 사용하여 원하는 것일 것입니다(이 데모에서는 cat file대신 사용됨).systemctl

$ cat file |
    awk -v OFS='\t' '$4=="running"{hd=$1 OFS $2 OFS $3 OFS $4; sub(/^([^ ]+ +){4}/,""); print hd, $0}' file |
    column -s$'\t' -t
init.scope                  loaded  active  running  System and Service Manager
session-23.scope            loaded  active  running  Session 23 of user admin
auditd.service              loaded  active  running  Security Auditing Service
chronyd.service             loaded  active  running  NTP client/server
crond.service               loaded  active  running  Command Scheduler
dbus.service                loaded  active  running  D-Bus System Message Bus
[email protected]          loaded  active  running  Getty on tty1
gssproxy.service            loaded  active  running  GSSAPI Proxy Daemon
irqbalance.service          loaded  active  running  irqbalance daemon
named.service               loaded  active  running  Berkeley Internet Name Domain (DNS)
NetworkManager.service      loaded  active  running  Network Manager
nfs-idmapd.service          loaded  active  running  NFSv4 ID-name mapping service
nfs-mountd.service          loaded  active  running  NFS Mount Daemon
nfsdcld.service             loaded  active  running  NFSv4 Client Tracking Daemon
oddjobd.service             loaded  active  running  privileged operations for unprivileged applications
polkit.service              loaded  active  running  Authorization Manager
postfix.service             loaded  active  running  Postfix Mail Transport Agent
rpc-gssd.service            loaded  active  running  RPC security service for NFS client and server
rpc-statd.service           loaded  active  running  NFS status monitor for NFSv2/3 locking.
rpcbind.service             loaded  active  running  RPC Bind
rsyslog.service             loaded  active  running  System Logging Service
[email protected]  loaded  active  running  Serial Getty on ttyS1
smartd.service              loaded  active  running  Self Monitoring and Reporting Technology (SMART) Daemon

내가 추측하는 출력은 systemctl질문에 제공하지 않았기 때문에 공백으로 구분된 문자열일 뿐입니다. 설명의 일부에 탭 문자가 포함될 수 있는 경우 탭 문자 gsub(OFS," ")앞에 출력 및 입력 필드 구분 기호 print로 사용할 수 없는 탭 문자 이외의 다른 문자를 찾으십시오 .awkcolumn

관련 정보