eddyP23
eddyP23

Reputation: 6853

How do I grep for a string excluding some other string

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?

Upvotes: 0

Views: 43

Answers (2)

Raimundo
Raimundo

Reputation: 605

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

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

Upvotes: 2

user4832408
user4832408

Reputation:

The only POSIX supported options for grep are -EFcefilnqsvx so be aware that the -A option may not be present on all implementations of grep. And even on GNU grep there is no option to specify "match OR match" and there is no regex that can emulate this as all it can do is provide additional matches, but can not withhold them. Essentially the only way to accomplish this with grep alone is to use a pipe.

Upvotes: 0

Related Questions