如何解析固定行数,如果关键字匹配则显示前2行

时间:2019-07-12 19:06:03

标签: bash

我对bash还是很陌生,但确实需要帮助, 我正在解析一个具有相同strucutre的日志文件,我需要每7行进行一次解析,如果关键字“ failed”匹配,则显示前2行,然后进行接下来的7行。

如果我尝试手动进行操作,将是这样,但随后我仅限于关键字 “ cat multipath_output | grep -B3'faulty'| grep -A1 DGC”

日志文件为

mpathes (3600601601a003200eca1345cf13ce511) dm-84 DGC,VRAID
size=800G features='0' hwhandler='1 alua' wp=rw
|-+- policy='round-robin 0' prio=0 status=enabled
| |- 0:0:1:147 sdtu  65:704   failed faulty running
| `- 1:0:0:147 sdtx  65:752   failed faulty running
`-+- policy='round-robin 0' prio=0 status=enabled
  |- 1:0:1:147 sdadh 65:816   failed faulty running
  `- 0:0:0:147 sdadi 65:832   failed faulty running
mpathdn (3600601601a00320024631529381ee511) dm-50 DGC,VRAID
size=200G features='0' hwhandler='1 alua' wp=rw
|-+- policy='round-robin 0' prio=0 status=enabled
| |- 0:0:0:114 sdhi  133:128  failed faulty running
| `- 1:0:1:114 sdib  134:176  failed faulty running
`-+- policy='round-robin 0' prio=0 status=enabled
  |- 0:0:1:114 sdqn  132:368  failed faulty running
  `- 1:0:0:114 sdrs  134:352  failed faulty running
mpathf (3600601601a00320078b040c86b38e211) dm-19 DGC,RAID 5
size=60G features='0' hwhandler='1 alua' wp=rw
|-+- policy='round-robin 0' prio=130 status=active
| |- 0:0:1:0   sdim  135:96   active ready  running
| `- 1:0:0:0   sdke  66:288   active ready  running
`-+- policy='round-robin 0' prio=10 status=enabled
  |- 0:0:0:0   sda   8:0      active ready  running
  `- 1:0:1:0   sdj   8:144    active ready  running
matchg (3600601601a003200e2efb15f8442e511) dm-257 DGC,VRAID
size=200G features='0' hwhandler='1 alua' wp=rw
|-+- policy='round-robin 0' prio=0 status=enabled
| |- 0:0:1:205 sdye  128:736  failed faulty running
| `- 1:0:0:205 sdyk  129:576  failed faulty running
`-+- policy='round-robin 0' prio=0 status=enabled
  |- 1:0:1:205 sdaht 128:880  failed faulty running
  `- 0:0:0:205 sdahu 128:896  failed faulty running

输出应该是:

mpathes (3600601601a003200eca1345cf13ce511) dm-84 DGC,VRAID size=800G features='0' hwhandler='1 alua' wp=rw

mpathdn (3600601601a00320024631529381ee511) dm-50 DGC,VRAID
size=200G features='0' hwhandler='1 alua' wp=rw

matchg (3600601601a003200e2efb15f8442e511) dm-257 DGC,VRAID
size=200G features='0' hwhandler='1 alua' wp=rw

"cat multipath_output | grep -B3   'faulty'  | grep -A1  DGC"

Script :-

FILES="multipath.txt"
for f in $FILES
do
        echo "$f"
        cat $f
        # Logic needs to go here

done

1 个答案:

答案 0 :(得分:0)

如果可以在awk中执行此操作(从bash外壳,大声笑),则应该可以进行以下操作。我会稍微重写一下规则:

  1. “块”的开始是当第一个字符是“ m”时
  2. 我们要捕获块的第1行和第2行, 块中包含“故障”一词
  3. 如果前一个块有问题,我们将显示第1行和第2行。

单线:

awk '/^m/ {if (fault) {print line1 " " line2} ; line1=$0 ; fault=0} ; /^size/ {line2=$0} ; /faulty/ {fault=1} ; END {if (fault=1) {print line1 " " line2}}' t

或逐行

  1. / ^ m / {if(故障){print line1“” line2}; line1 = $ 0; fault = 0}
  2. / ^ size / {line2 = $ 0}
  3. / faulty / {fault = 1}
  4. END {如果(故障){打印第1行“”第2行}}

[1] =当行以m开头时,抓住行1。如果前一个块有故障,请显示第1行和第2行。当我们得到第一个块时,错误就不会成立。

[2] =当行以大小开头时抓取第2行。

[3] =如果块中有故障,请将标志设置为true

[4] =由于我们在一个块的末尾显示了故障,因此在最后一个块之后检查是否存在故障。

mpathes (3600601601a003200eca1345cf13ce511) dm-84 DGC,VRAID size=800G features='0' hwhandler='1 alua' wp=rw
mpathdn (3600601601a00320024631529381ee511) dm-50 DGC,VRAID size=200G features='0' hwhandler='1 alua' wp=rw
matchg (3600601601a003200e2efb15f8442e511) dm-257 DGC,VRAID size=200G features='0' hwhandler='1 alua' wp=rw