シェルコマンドの出力形式を列として指定し、生成される列数を制限するにはどうすればよいですか?

シェルコマンドの出力形式を列として指定し、生成される列数を制限するにはどうすればよいですか?

私が走ると言う 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

同様の出力を維持しながら「説明」列をフォーマットして、スペースに基づいてより多くの列に分割しないようにするにはどうすればよいですか。たとえば、元の出力と同様に、読みやすい方法で5番目の列をフォーマットするにはどうすればよいですか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

関連情報