使用awk解析iostat输出

我正在尝试使用awk从 read 行中过滤参数 avgserv 的输出。

我的iostat命令PR_ATTACH_NUM的默认输出如下:

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 -------------------------------------------------------------------------------- 我设法打印出符合以下条件的行: avgserv

iostat -D hdisk0 | awk '/avgserv/'

但是

首先,我只返回标头,没有实际值。

第二, 我只需要为 read 行返回 avgserv 参数。不用于写行。

我的结局输出应仅包含 avgserv 参数的值,并且仅包含 read 行:

  

0.8

经过一番挖掘, 我设法使用bash-4.4$ iostat -D hdisk0 | awk '/avgserv/' read: rps avgserv minserv maxserv timeouts fails write: wps avgserv minserv maxserv timeouts fails

仅返回了 avgserv 参数。

但是, 我正在获取两行(读和写)所需的参数,并且又没有实际值。

chenglong767 回答:使用awk解析iostat输出

请您尝试以下。

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
本文链接:https://www.f2er.com/3072609.html

大家都在问