您需要告诉 grep 将该字符串用作固定字符串。为此,您可以使用-F
:
grep WARN log/* | grep -v -F 'This is a sentence [WARN]'
来自man grep
:
-F,--固定字符串
将 PATTERN 解释为固定字符串的列表,由换行符分隔,其中任何一个都将被匹配。(-F 由 POSIX 指定。)
测试
$ cat a
This is a sentence [LOG]
This is a sentence [WARN]
$ grep -v "sentence [LOG]" a <---- [LOG] is taken as regex
This is a sentence [LOG]
This is a sentence [WARN]
$ grep -Fv "sentence [LOG]" a <---- [LOG] is taken literally
This is a sentence [WARN]
或使用更新的输入:
$ cat a
aaaabbbbccccThis is a sentence [WARN]ccccdddd
aaaabbbbccccThis is a sentence [LOG]ccccdddd
hello!
$ grep -vF "sentence [WARN]" a
aaaabbbbccccThis is a sentence [LOG]ccccdddd
hello!