Thursday, October 28, 2021

[SOLVED] How do I grep for a string excluding some other string

Issue

I want to do sth like:

grep -A 10 'myString' && NOT 'anotherString'

If I didn't need -A 10 I know I could pipe greps and use -v, but it would not work like that in this case. So I would do sth like that:

grep "myString" | grep -v "anotherString"

Any ideas?


Solution

Try to invert and place the grep with the -A 10 argument in the end. Like this:

grep -v 'anotherString' | grep -A 10 'myString'


Answered By - Raimundo