我正在尝试使用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
但是
首先,我只返回标题,没有实际值。
第二,我只需要为read行返回avgserv参数。不用于写行。
我的结局输出应仅包含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.
从相对侧]进行短距离捕获:]