Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-cloud-platform/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Grep用于多个文件上的多个模式_Grep - Fatal编程技术网

Grep用于多个文件上的多个模式

Grep用于多个文件上的多个模式,grep,Grep,我一直在谷歌上搜索,但我找不到我想要的答案 假设我在目录mydir中有一个文件text1.txt,其内容是: one two two three four 另一个名为text2.txt,也在mydir中,其内容如下: one two two three four 我正在尝试获取一个文件列表(针对给定目录),其中包含我搜索的所有(而不是任何)模式。在我提供的示例中,我正在寻找以下内容的输出: ./text1.txt 或 我唯一能找到的是关于匹配文件中的任何模式,或者匹配单个文件中的多个模

我一直在谷歌上搜索,但我找不到我想要的答案

假设我在目录
mydir
中有一个文件
text1.txt
,其内容是:

one
two
two
three
four
另一个名为
text2.txt
,也在
mydir
中,其内容如下:

one
two
two
three
four
我正在尝试获取一个文件列表(针对给定目录),其中包含我搜索的所有(而不是任何)模式。在我提供的示例中,我正在寻找以下内容的输出:

./text1.txt

我唯一能找到的是关于匹配文件中的任何模式,或者匹配单个文件中的多个模式(我尝试将其扩展到整个目录,但收到grep使用错误)

非常感谢您的帮助

编辑我尝试过的东西

grep "pattern1" < ./* | grep "pattern2" ./*

返回与任一模式匹配的文件

一种方式如下:

find . | xargs grep 'pattern1' -sl | xargs grep 'pattern2' -sl

我认为这是您需要的(您可以轻松添加更多模式)


要完善大脑的答案:

find . -type f -print0 | xargs -0 grep 'pattern1' -slZ | xargs -0 grep 'pattern2' -sl

这将阻止grep尝试搜索目录,并且可以正确地处理带有空格的文件名,如果您将-Z标志传递给grep(最后一个模式除外),并将-0传递给xargs。

使用错误消息是什么?你试过的命令是什么?那是一个or,OP想要一个和或类似的替代:
grep-rl'pattern1'| xargs grep'pattern2'-l
grep-rlZ'pattern1'| xargs--null grep'pattern2'-l
对文件名更加安全。
find . -type f -print0 | xargs -0 grep 'pattern1' -slZ | xargs -0 grep 'pattern2' -sl