说我跑
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
如何维护类似的输出,但格式化“DESCRIPTION”列,以便它不会根据空间将其拆分为更多列?例如,如何以可读的方式格式化第五列,就像原始输出一样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
或找出除制表符之外的一些其他字符,这些字符不能用作awk
输出和column
输入字段分隔符。