D
D
Dima2016-05-20 21:32:04
bash
Dima, 2016-05-20 21:32:04

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...

separates only the 3 lines I need:
xxx...
yyy...
ERROR...

then I use the command,
grep 'xxx.*(/n(?!xxx).*)*ERROR:.*' log.txt
but the problem is that on bash it is no longer possible to pull out 3 lines. Probably because I'm using grep which handles single lines? Is there an alternative solution?.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2016-05-20
@Di9

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.

Which allow you to display the context surrounding the string

A
abcd0x00, 2016-05-21
@abcd0x00

Most likely, you are trying to solve the problem from the wrong side. It is necessary not to search for ERROR, and then restore the line, but first compose each individual line and then, if there is an ERROR, output it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question