问题描述
我有一个日志文件充满了查询,我只想看到有错误的查询。日志条目如下:
I have a log file full of queries, and I only want to see the queries that have an error. The log entries look something like:
path to file executing query
QUERY
SIZE: ...
ROWS: ...
MSG: ...
DURATION: ...
b $ b
我想打印所有这些东西,但只有当 MSG:
包含感兴趣的东西(错误消息)。我现在所有的是 sed -n'/ ^路径文件/,/ ^ DURATION /'
,我不知道从这里去。
I want to print all of this stuff, but only when MSG:
contains something of interest (an error message). All I've got right now is the sed -n '/^path to file/,/^DURATION/'
and I have no idea where to go from here.
注意:查询通常是多行的,所以使用grep的 -B
可悲的是不会一直工作到目前为止我一直在做,只是慷ous的 -B
值)
Note: Queries are often multiline, so using grep's -B
sadly doesn't work all the time (this is what I've been doing thus far, just being generous with the -B
value)
喜欢只使用 sed
,但如果我绝对必须使用 awk
我想这很好。
Somehow I'd like to use only sed
, but if I absolutely must use something else like awk
I guess that's fine.
谢谢!
推荐答案
你没有说错误信息,因此我假设它包含ERROR这个词:
You haven't said what an error message looks like, so I'll assume it contains the word "ERROR":
sed -n '/^MSG.*ERROR/{H;g;N;p;};/^DURATION/{s/.*//;h;d;};H' < logname
(我希望有一个更整洁的方法来清除保留空间。
(I wish there were a tidier way to purge the hold space. Anyone?...)
这篇关于当模式在范围内时,使用sed打印范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!