-1

Say I run systemctl | grep running | column -t

I get an output that looks like this:

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
getty@tty1.service               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
serial-getty@ttyS1.service       loaded  active  running  Serial         Getty          on          ttyS1
smartd.service                   loaded  active  running  Self           Monitoring     and         Reporting     Technology    (SMART)   Daemon

How can I maintain a similar output, but format the "DESCRIPTION" column so it's not splitting it into more columns based on the space? E.g. how do I format the 5th column in a readable manner like in the original output of systemctl?

I have looked at this question which got me so close: https://stackoverflow.com/questions/6462894/how-can-i-format-the-output-of-a-bash-command-in-neat-columns

1
  • 4
    Do you really need grep in this context? How about systemctl list-units --state=running Feb 23 at 23:02

1 Answer 1

0

This may be what you want, using any POSIX awk (and using cat file in place of systemctl for this demo):

$ 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
getty@tty1.service          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
serial-getty@ttyS1.service  loaded  active  running  Serial Getty on ttyS1
smartd.service              loaded  active  running  Self Monitoring and Reporting Technology (SMART) Daemon

I'm guessing that the output of systemctl is just blank-separated strings since you didn't provide it in your question. If any part of the description can contain a tab then add gsub(OFS," ") before the print or figure out some other char than tab that can't be present to use as the awk output and column input field separator.

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.