我想明白之间的差别grep -e
和grep -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
允许使用多个字符串进行搜索:'grep -e 'abc' -e 'def' -e '123'
将查找以下三个字符串中的任何一个:abc
以及def
和123
。
这与grep 'abc\|def\|123'
where\|
代表的工作方式非常相似,or
但阅读起来可能会更清晰一些。
由于grep -E
此处已经解释了最重要的事实,因此我只想在一个非常相似的问题上添加我对该主题的总结:Regular Expression for find double characters in Bash