从我们包含一堆 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
但是这些在输出中带来了tag1: valuep,它开始时没有匹配的结束模式。
从您的示例中,没有理由测试
This is the matching pattern
,只是结束正则表达式: