Using grep with regular expression to filter out matches

后端 未结 2 2045
陌清茗
陌清茗 2021-01-30 10:21

I\'m trying to use grep with -v for invert-match along with -e for regular expression. I\'m having trouble getting the syntax right.

I\'m trying something like

2条回答
  •  旧巷少年郎
    2021-01-30 11:08

    The problem is that by default, you need to escape your |'s to get proper alternation. That is, grep interprets "foo|bar" as matching the literal string "foo|bar" only, whereas the pattern "foo\|bar" (with an escaped |) matches either "foo" or "bar".

    To change this behavior, use the -E flag:

    tail -f logFile | grep -vE 'string one|string two'
    

    Alternatively, use egrep, which is equivalent to grep -E:

    tail -f logFile | egrep -v 'string one|string two'
    

    Also, the -e is optional, unless your pattern begins with a literal hyphen. grep automatically takes the first non-option argument as the pattern.

提交回复
热议问题