我正试图使用awk从read行过滤参数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
但是,
首先,我只返回Headers,没有返回Actual Values。
第二,我需要返回avgserv参数,仅用于read行。不适用于写入行。
我的最终输出应该只包含avgserv参数的值,并且只包含
read行的值:
0.8
经过一番挖掘,我使用iostat -D hdisk0 | awk '/avgserv/ {print $3}'
只返回了avgserv参数
但是,我得到了这两行(读和写(所需的参数,而且没有实际值。
请您尝试以下操作。
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.
对面的短捕获:
$ iostat -D hdisk0 | awk '/write: +.*avgserv/{ print v; exit }{ v=$2 }'
0.8