如何将两个搜索词与"grep"(AND)组合

Pet*_*ter 2 linux grep

使用grep,我可以在本地文件夹中的50个文件中找到一个单词,即:

grep -i "hello" *.html
Run Code Online (Sandbox Code Playgroud)

但是如何找到包含两个单词的文件?示例:我想查找包含Word"hello"和单词"peter"的所有文件.我怎样才能结合两个grep?

phs*_*phs 6

要查看包含两个单词的文件(可能在不同的行上),请使用-l和xargs:

grep -il "hello" *.html | xargs grep -il "peter"
Run Code Online (Sandbox Code Playgroud)

编辑

如果您的文件名称中有空格,那么我们需要更加小心.为此我们可以使用特殊选项grep和xargs:

grep -ilZ "hello" *.html | xargs -0 grep -il "peter"
Run Code Online (Sandbox Code Playgroud)