不匹配某个词,但如果其他词在字符串中则匹配

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

我有一个应该被 grep 过滤/不匹配的列表。假设我们有一个字符串:

This is a keyword string, that should not match

所以在这种情况下应该过滤行,因为包含 keyword.

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

本例中因为字符串中包含important这个词,所以应该匹配而不是过滤掉。还有一堆词,不只是important,还有像expensiveattention

这样的词

keyword 在字符串中总是在 important 之前。

为了简单起见,我试过:

echo "This is a keyword string, that should not match" | grep -i --invert 'keyword' 到目前为止这有效,输出为空。

然后玩弄背后的消极表情,我试过了

echo "This is a keyword string, that should not match" | grep -i --invert 'keyword.*?(?!important)' 但是那匹配。一引入.*?,就会匹配:

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

希望这只能通过 grep 实现。否则几行bash代码也是可以接受的。

更新取消 -P 选项: echo "This is a keyword string, that should not match, but now important is included" | grep -i --invert -P 'keyword.*?(?!important).*?' returns 一个空字符串

使用 awk 可能会更好。

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

如果你有 GNU grep,我想这样的东西也可以工作:

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