如何删除匹配 foo 但不匹配 bar 的行?

qua*_*gar 2 text-processing regular-expression

假设我想删除以下文件中的第 2 行和第 3 行,而不是第 4 行(那些匹配 foo 但不匹配 bar 的行),我可以用例如 sed 轻松地做到这一点吗?

text
foo some more text
even more foo text
some foo text with bar
Run Code Online (Sandbox Code Playgroud)

qua*_*gar 5

这似乎对我有用:

sed '/foo/{/bar/!d;}' my_file
Run Code Online (Sandbox Code Playgroud)