Hannes
Hannes

Reputation: 316

Do not match on certain word, but match if other word is in string

I have a list which should be filtered / not matched by grep. Lets say we have a string:

This is a keyword string, that should not match

So in this case line should be filtered, because of containing keyword.

This is another keyword string, that should match because important is now inside.

In this case because the word important is contained in the string, it should match and not be filtered. Also there is a bunch of words, not just important, but also words like expensive, attention.

keyword is always before important in the string.

To start easy, I've tried:

echo "This is a keyword string, that should not match" | grep -i --invert 'keyword' This works so far, output is empty.

Then to play around with negative look behind, I've tried

echo "This is a keyword string, that should not match" | grep -i --invert 'keyword.*?(?!important)' But that matches. As soon as .*? is introduced, it will match:

echo "This is a keyword string, that should not match" | grep -i --invert 'keyword.*?'
This is a keyword string, that should not match

Hope this is possible via grep only. Otherwise some lines of bash code are also acceptable.

Update unsing -P option: echo "This is a keyword string, that should not match, but now important is included" | grep -i --invert -P 'keyword.*?(?!important).*?' returns an empty string

Upvotes: 0

Views: 113

Answers (1)

oguz ismail
oguz ismail

Reputation: 50775

You'd probably have better luck with awk.

awk '/keyword/ && !/attention|expensive|important/ { next } 1'

If you have GNU grep, I guess something like this would also work:

grep -P -v 'keyword(?!.*(attention|expensive|important))'

Upvotes: 2

Related Questions