Linux 多重grep搜索

Linux 多重grep搜索,linux,grep,Linux,Grep,我尝试了多个grep搜索,但由于某些原因,它不起作用: 输入: What time is it in India Time in Israel Dogs are awesome I want chocolate cake 期望输出: What time is it in India chocolate cake 我使用命令: grep "(What time is it)|(chocolate cake)" inputfile.txt 然而,我得到的是一个空的输出。你知道为什么会出错吗?使

我尝试了多个grep搜索,但由于某些原因,它不起作用:

输入:

What time is it in India
Time in Israel
Dogs are awesome
I want chocolate cake
期望输出:

What time is it in India
chocolate cake
我使用命令:

grep "(What time is it)|(chocolate cake)" inputfile.txt

然而,我得到的是一个空的输出。你知道为什么会出错吗?

使用
egrep
而不是
grep
<代码>grep不理解您使用的regexp:

$ egrep "(what time is it)|(chocolate cake)" input.txt 
what time is it in India
i want chocolate cake
更准确地说,在现代类Unix系统上,
man grep
告诉我们:

在基本正则表达式中,元字符?、+、{、|、(、和) 失去它们的特殊意义;改为使用反斜杠版本\?, +,{,\\|,(,和)

因此,以下操作将产生相同的结果:

grep "\(what time is it\)\|\(chocolate cake\)" input.txt

使用
egrep
而不是
grep
grep
不理解您使用的regexp:

$ egrep "(what time is it)|(chocolate cake)" input.txt 
what time is it in India
i want chocolate cake
更准确地说,在现代类Unix系统上,
man grep
告诉我们:

在基本正则表达式中,元字符?、+、{、|、(、和) 失去它们的特殊意义;改为使用反斜杠版本\?, +,{,\\|,(,和)

因此,以下操作将产生相同的结果:

grep "\(what time is it\)\|\(chocolate cake\)" input.txt

您必须转义管道(
|
),因为它是一个特殊字符

grep "what time is it\|chocolate cake" inputfile.txt
你的正则表达式中的paren是多余的,可以省略。如果你省略了它们,它们也必须被省略:

grep "\(what time is it\)\|\(chocolate cake\)" inputfile.txt

您必须转义管道(
|
),因为它是一个特殊字符

grep "what time is it\|chocolate cake" inputfile.txt
你的正则表达式中的paren是多余的,可以省略。如果你省略了它们,它们也必须被省略:

grep "\(what time is it\)\|\(chocolate cake\)" inputfile.txt

很好的问题。很好的标题和一个很好的例子!很好的问题可能重复!很好的标题和一个很好的例子!可能重复或类似,grep-e'string1'-e'string2'input.txt或类似,grep-e'string1'-e'string2'input.txt