我试图使用awk从读取行过滤参数avgserv的输出。
iostat命令的默认输出:iostat -D hdisk0如下所示:
bash-4.4$ iostat -D hdisk0
System configuration: lcpu=32 drives=9 paths=126 vdisks=0
hdisk0 xfer: %tm_act bps tps bread bwrtn
0.0 3.0K 0.1 98.3 2.9K
read: rps avgserv minserv maxserv timeouts fails
0.0 0.8 0.0 0.0 0 0
write: wps avgserv minserv maxserv timeouts fails
0.1 2.2 0.0 0.0 0 0
queue: avgtime mintime maxtime avgwqsz avgsqsz sqfull
0.0 0.0 0.0 0.0 0.0 0.0
--------------------------------------------------------------------------------使用:iostat -D hdisk0 | awk '/avgserv/',我成功地打印了匹配的行:avgserv
bash-4.4$ iostat -D hdisk0 | awk '/avgserv/'
read: rps avgserv minserv maxserv timeouts fails
write: wps avgserv minserv maxserv timeouts fails但,
首先,我只返回标题,而不返回实际值。
其次,我需要返回avgserv参数,只用于读行。不是为了写台词。
我的最终输出应该只包含avgserv参数的值,并且只包含读取行的值:
0.8
经过一番研究,我成功地返回了avgserv参数,使用:iostat -D hdisk0 | awk '/avgserv/ {print $3}'
但是,我仍然获得了这两行(读和写)所需的参数,而且同样没有实际值。
发布于 2019-11-19 14:50:55
你能试一下吗。
your_command |
awk '
/avgserv/ && /read/{
found=1
next
}
found{
print $2
found=""
}'一种线性解的形式:
your_command | awk '/avgserv/ && /read/{found=1;next} found{print $2;found=""}'解释:添加对上述代码的解释。
your_command | ##Sending your command output as standard input to awk command.
awk ' ##Starting awk command from here.
/avgserv/ && /read/{ ##Checking condition if a line has string avgserv AND read then do following.
found=1 ##Setting variable found value to 1 here.
next ##next will skip all further statements from here.
} ##Closing BLOCK for above condition here.
found{ ##Checking condition if found is NOT NULL then do following.
print $2 ##Printing 2nd field here.
found="" ##Nullifying variable found here.
}' ##Closing BLOCK for found condition here.发布于 2019-11-19 15:08:12
对岸短距离捕捉
$ iostat -D hdisk0 | awk '/write: +.*avgserv/{ print v; exit }{ v=$2 }'
0.8https://stackoverflow.com/questions/58936641
复制相似问题