【发布时间】:2017-04-08 16:00:50
【问题描述】:
我有一个日志文件,我需要属于特定类型日志的部分。它可以是多行。
我不能直接在这里发布日志文件,但它的格式如下:
<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-MESSAGE1' 作为开始和结束模式都不起作用,如果有两个连续的日志带有 '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}' 之后添加花括号,我得到了 'extra characters after command' 错误。 有更好的选择吗?或者有没有办法可以在 sed 中使用花括号对命令进行分组?
【问题讨论】:
标签: regex perl parsing awk sed