我在Linux机器上加载了大量Java日志,并且试图找到grep表达式或其他内容(perl,awk),该表达式可以在匹配项的整个正文中为我提供整个日志条目。Logstash看起来可以完成任务,但是使用板载工具会更好。
一个例子应该最好。这是具有5个不同条目的示例性日志:
25 Aug 2016 14:00:46,435 DEBUG [User][IP][rsc] An error occurred
java.Exception: Foo1
at xyz
25 Aug 2016 14:00:46,436 Foo2 [User][IP][rsc] Some error occured
25 Aug 2016 14:00:46,436 DEBUG [User][IP][rsc] Somethin occured Foo3
25 Aug 2016 14:18:18,224 XYZ [User][IP][rsc] Some problems
More: bla1
More: bla2
USER.bla.bla: Blala::123 - 456
More: Could not open something
at 567
at 890
Caused by: Foo4: Could not open another thing
at 123
at 456
... 127 more
Caused by: gaga
at a1a2a3
at b3b3b3
... 146 more
25 Aug 2016 14:18:20,118 SSO [User][IP][rsc] Process: error -
Could not Foo5
<here is a blank line>
当我搜索“ Foo1”时,我需要:
25 Aug 2016 14:00:46,435 DEBUG [User][IP][rsc] An error occurred
java.Exception: Foo1
at xyz
当我搜索“ Foo2”时:
25 Aug 2016 14:00:46,436 Foo2 [User][IP][rsc] Some error occured
对于“ Foo3”:
25 Aug 2016 14:00:46,436 DEBUG [User][IP][rsc] Somethin occured Foo3
对于“ Foo4”:
25 Aug 2016 01:18:18,224 XYZ [User][IP][rsc] Some problems
More: bla1
More: bla2
USER.bla.bla: Blala::123 - 456
More: Could not open connection
at 567
at 890
Caused by: Foo4: Could not open connection
at 123
at 456
... 127 more
Caused by: gaga
at a1a2a3
at b3b3b3
... 146 more
最后是“ Foo5”:
25 Aug 2016 01:18:20,118 SSO [User][IP][rsc] Process: error -
Could not Foo5
当我搜索“ Foo”时,应返回所有内容。这样的事情可能吗?也许甚至是一个班轮?我想在Webmin自定义命令模块中使用它,在这里我通过变量提供表达式。
我目前唯一的基本想法是搜索表达式,并使用“ [”作为模式来标识新条目的开始位置。
预先感谢任何有想法的人!
sed解决方案-适用于不允许awk的环境-相同的sed命令以oneliner和多行形式显示
pat=$1
# oneliner form
#sed -nr '/^[0-9]{2} [a-zA-Z]{3} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} /!{H; $!b}; x; /'"$pat"'/p; ${g; /^[0-9]{2} [a-zA-Z]{3} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} /!q; /'"$pat"'/p }'
# multiline form
sed -nr '
/^[0-9]{2} [a-zA-Z]{3} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} /!{H; $!b}
x
/'"$pat"'/p
${
g
/^[0-9]{2} [a-zA-Z]{3} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3} /!q
/'"$pat"'/p
}'
在行首使用时间戳作为记录开始-累积非时间戳行,即记录空间在保持空间中-在记录开始时交换保持空间和模式空间-在模式匹配时打印记录
记录从最后一行开始的特殊情况-必须从保持空间中重新获取它,并分别测试其模式匹配
用pat
bash变量构造sed命令所需的shell引用
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句