我是bash的新手,但真的需要帮助,我正在解析一个具有相同结构的日志文件,我需要每7行解析一次,如果关键字“失败”匹配,则显示前2行,然后继续下7行。
如果我尝试手动操作,结果会是这样的,但我只能使用关键字"cat multipath_output | grep -B3‘故障’| 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发布于 2019-07-13 03:40:02
如果在awk (从bash shell,LOL)中可以做到这一点,那么下面的方法应该可以。我会稍微重写一下规则:
”时
作为一行程序:
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或者是逐行
打印错误/^m/ {if () {print line1“”line2};line1=$0;fault=0}
如果行以m开头,则显示1=Grab line1。如果上一块出现故障,则显示line1和line2。当我们得到第一个块时,错误就不是真的了。
当行以大小开始时,使用2=Grab line2。
3=If块中有故障,请将标志设置为true
4=Since我们在一个块的末尾显示故障,在最后一个块之后检查是否有故障。
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=rwhttps://stackoverflow.com/questions/57012764
复制相似问题