我有一个日志文件,需要属于特定类型日志的部分。它可以是多行的。
我不能在这里直接发布日志文件,但它的格式如下:
<date-format> Thread-MESSAGE1 random-message
line 1
line 2
line 3
line 4
<date-format> Thread-MESSAGE1 random-message2
line 5
<date-format> Thread-MESSAGE2 random-message3
line 6
line 7
line 8
line 9
<date-format> Thread-MESSAGE3 random-message4
<date-format> Thread-MESSAGE1 random-message5
<date-format> Thread-MESSAGE1 random-message6
line 10
line 11
<date-format> Thread-MESSAGE7 random-message7
<date-format> Thread-MESSAGE8 random-message9
<date-format> Thread-MESSAGE9 random-message10
<date-format> Thread-MESSAGE1 random-message11 我需要的输出是:
<date-format> Thread-MESSAGE1 random-message
line 1
line 2
line 3
line 4
<date-format> Thread-MESSAGE1 random-message2
line 5
<date-format> Thread-MESSAGE1 random-message5
<date-format> Thread-MESSAGE1 random-message6
line 10
line 11
<date-format> Thread-MESSAGE1 random-message11 我试着使用sed,但是使用‘Thread 1’作为开始模式和结束模式,如果有两个连续的带有'MESSAGE1‘键的日志,则不能工作。
我曾想过使用Perl预先使用负查找(这是有效的),但不幸的是,我不能使用Perl,而且'sed‘和'awk’都不支持在模式中提前进行负查找。
最近,我尝试使用以下“sed”模式:
tac source_file.log | sed -n '{/<date-format> Thread-/!H; /<date-format> Thread-/{H;d;x} /<date-format> Thread-MESSAGE1/p; d;}' > test.log这样做的目的是后来反转test.log的输出,但是为了在'Thread-/{H;d;x}‘之后添加大括号,我将得到’命令后的额外字符‘错误。还有更好的选择吗?或者,我是否可以使用sed中的大括号对命令进行分组?
发布于 2016-11-24 11:18:14
您可以使用这个awk命令:
awk -v kw='Thread-MESSAGE1' '$2 ~ /^Thread-/ {p = ($2 == kw)} p' file
<date-format> Thread-MESSAGE1 random-message
line 1
line 2
line 3
line 4
<date-format> Thread-MESSAGE1 random-message2
line 5
<date-format> Thread-MESSAGE1 random-message5
<date-format> Thread-MESSAGE1 random-message6
line 10
line 11
<date-format> Thread-MESSAGE1 random-message11如果这不能锻炼,那么我建议你发布更真实的样本数据。
https://stackoverflow.com/questions/40784871
复制相似问题