从我们的一个包含一堆 INFO 消息和系统输出行的应用程序日志中,我希望能够提取包含在 2 个模式中的行,然后专门从结果行中的其他行获取值。
这是我的输入文件
2019-08-16 00:38:29,171 1065142892 [http-bio-8443-exec-146] INFO aaaaa
2019-08-16 00:38:29,172 1065142893 [http-bio-8443-exec-146] INFO bbbbb
'This is the matching pattern'
tag1: value1
tag2: value2
tag3: value3
'this is the end pattern' xxxyyyzzz
2019-08-16 00:39:29,171 1065142992 [http-bio-8443-exec-146] INFO aaaaa
2019-08-16 00:39:29,172 1065142993 [http-bio-8443-exec-146] INFO bbbbb
'This is the matching pattern'
tag1: valuea
tag2: valueb
tag3: valuec
'this is the end pattern' xxxyyyzzzadasd
2019-08-16 00:38:29,171 1065142892 [http-bio-8443-exec-146] INFO aaaaa
2019-08-16 00:38:29,172 1065142893 [http-bio-8443-exec-146] INFO bbbbb
'This is the matching pattern'
tag1: valuep
2019-08-16 01:38:29,171 1065153992 [http-bio-8443-exec-146] INFO aaaaa
2019-08-16 01:38:29,172 1065153993 [http-bio-8443-exec-146] INFO bbbbb
'This is the matching pattern1'
tag1: valuexx
tag2: valueyy
tag3: valuezz
'this is the end pattern' xxxyyyzzzadasdqwerty
由此,我想通过以下方式提取输出:
输出:
value1, value2
valuea, valueb
valuexx, valueyy
我尝试使用以下内容来过滤结果
awk '/This is the matching pattern/,/This is the end pattern/' logfile
OR
awk ' /This is the matching pattern/{flag=1;next}/This is the end pattern/{flag=0}flag' logfile
OR
sed -n -e '/This is the matching pattern/,/this is the end pattern/{ /This is the matching pattern/d; /this is the end pattern/d; p; }' logfile
但这些带来标签1:值p在输出中没有匹配的结束模式开始。
答案1
从您的示例来看,没有理由测试This is the matching pattern
,只需测试结尾的正则表达式:
$ cat tst.awk
BEGIN { FS=": "; OFS=", " }
{ f[$1] = $2 }
/this is the end pattern/ {
print f["tag1"], f["tag2"]
delete f
}
$ awk -f tst.awk file
value1, value2
valuea, valueb
valuexx, valueyy