Oca*_*shu 13 unix bash grep regex
[Computer]$ grep "foo|bar" filename
Run Code Online (Sandbox Code Playgroud)
我知道上面的命令应该返回文件名中存在“foo”或“bar”的每一行。手册页确认 | 作为正则表达式或符号,代码独立用于“foo”和“bar”。我错过了什么?
jhe*_*ger 22
grep 默认使用基本正则表达式 (BRE)。从手册页:
基本与扩展正则表达式: 在基本正则表达式中,元字符 ?、+、{、|、( 和 ) 失去了它们的特殊含义;而是使用反斜杠版本 \?、+、{、\|、( 和 )。
所以你要么必须逃避|:
grep "foo\|bar" filename
Run Code Online (Sandbox Code Playgroud)
或打开扩展正则表达式:
grep -E "foo|bar" filename
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24414 次 |
| 最近记录: |