grep -e 和 grep -E 选项有什么区别?

ron*_*nie 49 grep

我想明白之间的差别grep -egrep -E。现在从grep manpage我得到:

-E, --extended-regexp

将 PATTERN 解释为扩展的正则表达式(见下文)。

-e 模式, --regexp=模式

使用 PATTERN 作为模式;有助于保护以 - 开头的模式

上面的解释对我来说没有意义。

那么,有人可以使用examples两者之间的区别以及何时使用哪个选项向我解释它。

PS:版本:grep (GNU grep) 2.10

haf*_*huk 40

-e严格来说是用于指示您要匹配的模式的标志。-E控制是否需要转义某些特殊字符。

man grep再解释-E一下:

   Basic vs Extended Regular Expressions
   In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

   Traditional  egrep  did  not  support  the  {  meta-character, and some egrep implementations support \{ instead, so portable scripts should avoid { in grep -E patterns and should use [{] to match a
   literal {.

   GNU grep -E attempts to support traditional usage by assuming that { is not special if it would be the start of an invalid interval specification.  For example, the command grep -E '{1' searches for
   the two-character string {1 instead of reporting a syntax error in the regular expression.  POSIX.2 allows this behavior as an extension, but portable scripts should avoid it.
Run Code Online (Sandbox Code Playgroud)

  • `grep -e -o 'h|l'` 将逐字匹配 `h|l` 而 `grep -e -o 'h\|l'` 将匹配 `h` 或 `l` 而 -E 则相反 (5认同)
  • 所以这就是我做`echo "hello" 的原因 | grep -o -e 'h|l'` 我没有输出,因为`|` 失去了它的特殊意义,当我做`echo "hello" | grep -o -E 'h|l'` 我得到了想要的输出。 (4认同)
  • 是的。`回声“你好” | grep -o -e 'h' -e 'l'` 如果你想在这种情况下扔掉 `-E` 就可以了。 (3认同)

erc*_*rch 9

grep -e允许使用多个字符串进行搜索:'grep -e 'abc' -e 'def' -e '123'将查找以下三个字符串中的任何一个:abc以及def123

这与grep 'abc\|def\|123'where\|代表的工作方式非常相似,or但阅读起来可能会更清晰一些。

由于grep -E此处已经解释了最重要的事实,因此我只想在一个非常相似的问题上添加我对该主题的总结:Regular Expression for find double characters in Bash