Answer the question
In order to leave comments, you need to log in
How to do multiline search in bash using complex regex?
I am analyzing a log file that contains lines of the form ERROR: ... but for correct analysis it is also necessary to get the command that led to the error, which is located one or more lines above the file. Compiled a regular expression that is processed in the editor as I need xxx.*(/n(?!xxx).*)*ERROR:.*
In the log:
xxx...
xxx...
xxx...
yyy...
ERROR...
xxx...
xxx...
xxx...
yyy...
ERROR...
grep 'xxx.*(/n(?!xxx).*)*ERROR:.*' log.txt
Answer the question
In order to leave comments, you need to log in
The grep command has three more options:
-A NUM, --after-context=NUM
Print NUM lines of trailing context after matching lines. Places a line containing a group
separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
-B NUM, --before-context=NUM
Print NUM lines of leading context before matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
-C NUM, -NUM, --context=NUM
Print NUM lines of output context. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question