使用awk和count结果解析命令输出

时间:2013-11-13 11:34:44

标签: bash parsing awk

我有'multipath -ll'命令的输出

来自RHEL:

mpath114 (3600507680283095ea8000000000004fa) dm-28 IBM,2145
[size=200G][features=1 queue_if_no_path][hwhandler=0][rw]
\_ round-robin 0 [prio=50][active]
 \_ 19:0:0:40 sdea 128:32  [active][ready]
 \_ 20:0:1:40 sdeb 128:48  [active][ready]
 \_ 20:0:1:41 sdec 128:16  [failed][faulty]
\_ round-robin 0 [prio=10][enabled]
 \_ 20:0:0:40 sdba 67:64   [active][ready]
 \_ 19:0:1:40 sdgg 131:192 [active][ready]
mpath131 (3600507680283095ea800000000000504) dm-39 IBM,2145
[size=10G][features=1 queue_if_no_path][hwhandler=0][rw]
\_ round-robin 0 [prio=50][active]
 \_ 20:0:1:1  sdbl 67:240  [active][ready]
 \_ 19:0:0:1  sdc  8:32    [active][ready]
\_ round-robin 0 [prio=10][enabled]
 \_ 19:0:1:1  sdet 129:80  [active][ready]
 \_ 20:0:0:1  sdk  8:160   [active][ready]
[..]

或者来自SLES服务器:

mpathmzp (36005076801c7061ef800000000000089) dm-0 IBM,2145
size=10G features='1 queue_if_no_path' hwhandler='0' wp=rw
|-+- policy='round-robin 0' prio=50 status=enabled
| `- 67:0:2:0 sde 8:64 active ready running
| `- 68:0:0:1 sdl 8:76 failed faulty running
`-+- policy='round-robin 0' prio=10 status=enabled
  |- 67:0:3:0 sdc 8:32 active ready running
  `- 68:0:0:0 sdd 8:48 active ready running
[..]

我想解析它(最好使用awk或bash),以显示配置摘要。 它应该打印伪多路径设备和活动路径的数量以及失败的(如果有的话) 样品:

dm-39, 10G,  Total: 4 paths, active: 4, failed: 0
dm-28, 200G, Total: 5 paths, active: 4, failed: 1

同样的SLES:

dm-0,  10G,  Total: 4 paths, active: 3, failed: 1

如果可能,我还要对输出进行排序,以便没有失败的路径和最活跃的路径位于顶部,并以带有失败路径的devicess结束。

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

awk应该:

multipath -ll | awk 'NR>1 {r=f=0;for (i=1;i<=NF;i++) if ($i~/ready/) r++; else if ($i~/faulty/) f++;split($5,a,"=|]");print $3,a[2]"\tTotal: "r+f" paths, active: "r,"failed: "f}' RS="mpath" OFS=", "
dm-28, 200G     Total: 5 paths, active: 4, failed: 1
dm-39, 10G      Total: 4 paths, active: 4, failed: 0