开发者

How to use 'grep' to match a string except when another string matches?

In the following file I just looking for a grep command:

Line 1 : string 1  (errno:1)
Line 2 : string 1  (errno:3)
Line 3 : string 1  (errno:1)
Line 4 : string 1  (errno:1)

It should match "string 1" and not equal to "errno:1". How can I do tha开发者_开发百科t using grep?


I'm sure there's a way of doing it all together, but the easiest way is just to chain multiple grep commands together

cat <myfile> | grep "string 1" | grep -v "errno:1"

The -v option inverts the search, so that will display the lines that have "string 1" without "errno:1"


Find any "string 1" that is not followed by "errno:1":

grep -P 'string 1(?!.*errno:1)' inputfile
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜