【发布时间】:2021-06-03 11:33:58
【问题描述】:
我在命令下运行,但它返回空。
grep -i -e "abc()|daf()|newname|" test.txt
test.txt 包含以下数据。
abc()
daf()
blahblah
newname
我的输出为空,但希望返回字符串,有人可以解释或分享为什么没有得到所需的输出。谢谢!!
【问题讨论】:
我在命令下运行,但它返回空。
grep -i -e "abc()|daf()|newname|" test.txt
test.txt 包含以下数据。
abc()
daf()
blahblah
newname
我的输出为空,但希望返回字符串,有人可以解释或分享为什么没有得到所需的输出。谢谢!!
【问题讨论】:
来自grep(1)man page;
基本与扩展正则表达式
在基本的正则表达式中,元字符?、+、{、|、(和)失去了它们的特殊含义;而是使用反斜杠版本?、+、{、|、(和)。
有 2 个选项;
使用扩展的正则表达式 -E 来“启用”|
grep -i -E "abc()|daf()|newname" test.txt
转义 | 以继续使用常规正则表达式 -e:
grep -i -e "abc()\|daf()\|newname" test.txt
这两个选项都会导致:
abc()
daf()
newname
【讨论】: