问题描述
假设我有一个文件 text1.txt
,目录 mydir
其内容为:
一个
两个
另一个叫做 text2.txt
,同样位于 mydir
中,其内容为:
两个
三个
四个
我正在尝试获取包含我搜索的所有(不是任何)模式的文件列表(对于给定的目录)。在我提供的例子中,我正在寻找输出:
./ text1.txt
或
./ text1.txt:one
./text1.txt:two
唯一我已经能够找到的东西是关于匹配文件中的任何模式,或匹配单个文件中的多个模式(我尝试扩展到整个目录,但收到了grep使用错误)。
非常感谢任何帮助。
我试过的编辑工具
greppattern1 < ./* | greppattern2./*
模棱两可的重定向
grep'pattern1'|'pattern2'./*
返回匹配任何模式的文件
一种方法可能是这样的:
find。 | xargs grep'pattern1'-sl | xargs grep'pattern2'-sl
I've been googling around, and I can't find the answer I'm looking for.
Say I have a file, text1.txt
, in directory mydir
whose contents are:
one
two
and another called text2.txt
, also in mydir
, whose contents are:
two
three
four
I'm trying to get a list of files (for a given directory) which contain all (not any) patterns I search for. In the example I provided, I'm looking for output somewhere along the lines of:
./text1.txt
or
./text1.txt:one
./text1.txt:two
The only things I've been able to find are concerning matching any patterns in a file, or matching multiple patterns in a single file (which I tried extending to a whole directory, but received grep usage errors).
Any help is much appreciated.
Edit-Things I've tried
grep "pattern1" < ./* | grep "pattern2" ./*
"ambiguous redirect"
grep 'pattern1'|'pattern2' ./*
returns files that match either pattern
One way could be like this:
find . | xargs grep 'pattern1' -sl | xargs grep 'pattern2' -sl
这篇关于Grep针对多个文件的多种模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!