【问题标题】:parse iostat output using awk使用 awk 解析 iostat 输出
【发布时间】:2020-03-15 03:01:17
【问题描述】:

我正在尝试使用 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

但是,

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

第二, 我需要返回 avgserv 参数,仅用于 read 行。不是为了写线。

我的结局输出应该只包含 avgserv 参数的值,并且只用于 read 行:

0.8

经过一番挖掘, 我设法只返回 avgserv 参数使用:iostat -D hdisk0 | awk '/avgserv/ {print $3}'

但是, 我得到了两条线(读取和写入)所需的参数,但又没有实际值。

【问题讨论】:

    标签: awk aix iostat


    【解决方案1】:

    请您尝试关注一下。

    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.
    

    【讨论】:

    • 你能解释一下你的代码吗?是否有单行命令的选项?
    • @edwio,已在我的解决方案中添加了说明 :) 当然让我现在添加单行表格。
    • @edwio,我现在也添加了一种线性解决方案,如果有任何疑问,请告诉我。
    • 为什么我们需要使用 found="" (在此处找到的无效变量)?我们已经打印了第二个字段的值
    • @edwio,这是因为它不应该打印除 avgservread 的下一行之外的任何内容(任何行),否则一旦它是 1 并且以后不清除它可能也打印不需要的结果,干杯。
    【解决方案2】:

    对面进行短截

    $ iostat -D hdisk0 | awk '/write: +.*avgserv/{ print v; exit }{ v=$2 }'
    0.8
    

    【讨论】:

    • 你能详细说明一下你所说的相反是什么意思吗?
    猜你喜欢
    • 2013-02-03
    • 2012-08-18
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    相关资源
    最近更新 更多