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: ...
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.
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)
Somehow I'd like to use only sed
, but if I absolutely must use something else like awk
I guess that's fine.
Thanks!
You haven't said what an error message looks like, so I'll assume it contains the word "ERROR":
(I wish there were a tidier way to purge the hold space. Anyone?...)
Perhaps you can use the cgrep.sed script, as described by Unix Power Tools book
I could suggest a solution with
grep
. That will work if the structure in the log file is always the same as above (i.e. MSG is in the 5th line, and one line follows):That means: If the word
error
occurs in aMSG
line then output the block beginning from 4 lines beforeMSG
till one line after it. Of course you have to adjust the regexp to recognize an error.This will not work if the structure of those blocks differs.