多个grepsearch

我正在尝试多个grepsearch,但由于某种原因,它不工作:

input:

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\|chocolate cake" inputfile.txt 

正则表达式中的parens是多余的,可以放弃。 如果你离开他们,他们也必须逃脱:

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

使用egrep而不是grepgrep不理解你使用的正则表达式:

 $ egrep "(what time is it)|(chocolate cake)" input.txt what time is it in India i want chocolate cake 

更确切地说,现代类Unix系统上的人们会说:

在基本的正则表达式中,元字符?,+,{,|,(和)失去了特殊的含义; 而是使用反斜杠\ \,+,{,\ |,(和)。

所以,下面的结果是一样的:

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