使用awk搜索结果为图形形成变量

时间:2014-12-22 20:32:31

标签: awk gnu

我使用cacti来绘制具有7个模块的设备的CPU使用情况,所使用的命令显示每个模块的12个样本。我需要使用awk来查找每个模块名称的模式,并在使用此sintaxis [模块]形成变量之后:[第12个CPU样本],例如:MSCBC05:47

下面提到的命令输出摘录:

ACT                               AD-46   TIME 141216 1556  MSCBC05
PROCESSOR LOAD DATA
INT PLOAD CALIM OFFDO OFFDI FTCHDO FTCHDI OFFMPH OFFMPL FTCHMPH FTCHMPL
 1   46   56250   656    30   656     30   1517      2   1517       2
 2   47   56250   659    32   659     32   1448      1   1448       1
 3   46   56250   652    22   652     22   1466      1   1466       1
 4   47   56250   672    33   672     33   1401      0   1401       0
 5   47   56250   674    38   674     38   1446      2   1446       2
 6   45   56250   669    22   669     22   1365      1   1365       1
 7   45   56250   674    26   674     26   1394      2   1394       2
 8   46   56250   664    24   664     24   1396      0   1396       0
 9   47   56250   686    24   686     24   1425      2   1425       2
10   47   56250   676    31   676     31   1386      0   1386       0
11   48   56250   702    25   702     25   1414      2   1414       2
12   47   56250   703    31   703     31   1439      2   1439       2

完成输出 https://dl.dropboxusercontent.com/u/33222611/raw_output.txt

1 个答案:

答案 0 :(得分:1)

我建议

awk '$1 == "ACT" { sub(/\r/, ""); curmsc = $6 } curmsc != "" && $1 == "12" { print curmsc ":" $2; curmsc = "" }' raw_output.txt

写得更可读,就是

$1 == "ACT" {    # In the first line of an ACT block
  sub(/\r/, "")  # remove the trailing carriage return. Could also use todos or so.
  curmsc = $6    # remember MSC
}
curmsc != "" && $1 == "12" {  # if we are in such a block and the first token is 12
  print curmsc ":" $2         # print the stuff we want to know
  curmsc = ""                 # then flag that we're outside a block
}