Grep doesn’t work with both -v and -A/B

grep

So I'm trying to use grep with both -v argument and -A argument.

So my file has text like:

This error
-this
-this
-that
[text I want]
This error
-asd
-asfag
-adsfhs
[text I want]
[text I want]
This error
-asdgsda
-asdgg
-gasdg

After a tail, I found out that this log file is filled with the same This error followed by 3 lines construct, and I can easily find them by using grep -A3 'This error', this shows me the error and the 3 lines after it.

But I want to see everything OTHER than that, hence the -v argument, but it doesn't work. When I do grep -v -A3 'This error' it returns the whole file, like grep didn't even work at all.

What is the problem here?

Best Answer

If you can use sed instead of grep, it provides a nice solution:

sed -e '/This error/,+3d' myfile

This removes the line containing the string This error and the following three lines, but outputs everything else. This sed command requires GNU sed (the +3 address is an extension).

Related Question