bash grep 用于文件中不同行的多个字符串(即整个文件,而不是基于行的搜索)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4795323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep for multiple strings in file on different lines (ie. whole file, not line based search)?
提问by Christian
I want to grep for files containing the words Dansk
, Svenska
or Norsk
on any line, with a usable returncode (as I really only like to have the info that the strings are contained, my one-liner goes a little further then this).
我想 grep 包含单词的文件Dansk
,Svenska
或 Norsk
在任何行上,使用可用的返回码(因为我真的只喜欢包含字符串的信息,我的单行比这更进一步)。
I have many files with lines in them like this:
我有很多文件,里面有这样的行:
Disc Title: unknown
Title: 01, Length: 01:33:37.000 Chapters: 33, Cells: 31, Audio streams: 04, Subpictures: 20
Subtitle: 01, Language: ar - Arabic, Content: Undefined, Stream id: 0x20,
Subtitle: 02, Language: bg - Bulgarian, Content: Undefined, Stream id: 0x21,
Subtitle: 03, Language: cs - Czech, Content: Undefined, Stream id: 0x22,
Subtitle: 04, Language: da - Dansk, Content: Undefined, Stream id: 0x23,
Subtitle: 05, Language: de - Deutsch, Content: Undefined, Stream id: 0x24,
(...)
Here is the pseudocode of what I want:
这是我想要的伪代码:
for all files in directory;
if file contains "Dansk" AND "Norsk" AND "Svenska" then
then echo the filename
end
What is the best way to do this? Can it be done on one line?
做这个的最好方式是什么?可以在一条线上完成吗?
回答by vmpstr
You can use:
您可以使用:
grep -l Dansk * | xargs grep -l Norsk | xargs grep -l Svenska
If you want also to find in hidden files:
如果您还想在隐藏文件中查找:
grep -l Dansk .* | xargs grep -l Norsk | xargs grep -l Svenska
回答by Edd Steel
Yet another way using just bash and grep:
另一种只使用 bash 和 grep 的方法:
For a single file 'test.txt':
对于单个文件“test.txt”:
grep -q Dansk test.txt && grep -q Norsk test.txt && grep -l Svenska test.txt
Will print test.txt
iff the file contains all three (in any combination). The first two greps don't print anything (-q
) and the last only prints the file if the other two have passed.
test.txt
如果文件包含所有三个(任意组合),将打印。前两个 grep 不打印任何内容 ( -q
),最后一个仅在其他两个通过时才打印文件。
If you want to do it for every file in the directory:
如果要对目录中的每个文件执行此操作:
for f in *; do grep -q Dansk $f && grep -q Norsk $f && grep -l Svenska $f; done
回答by Gerry
grep –irl word1 * | grep –il word2 `cat -` | grep –il word3 `cat -`
-i
makes search case insensitive-r
makes file search recursive through folders-l
pipes the list of files with the word foundcat -
causes the next grep to look through the files passed to it list.
-i
使搜索不区分大小写-r
使文件搜索通过文件夹递归-l
管道文件列表与找到的词cat -
导致下一个 grep 查看传递给它的文件列表。
回答by Damodharan R
How to grep for multiple strings in file on different lines (Use the pipe symbol):
如何 grep 文件中不同行的多个字符串(使用管道符号):
for file in *;do
test $(grep -E 'Dansk|Norsk|Svenska' $file | wc -l) -ge 3 && echo $file
done
Notes:
笔记:
If you use double quotes
""
with your grep, you will have to escape the pipe like this:\|
to search for Dansk, Norsk and Svenska.Assumes that one line has only one language.
如果
""
在 grep 中使用双引号,则必须像这样转义管道:\|
搜索 Dansk、Norsk 和 Svenska。假设一行只有一种语言。
Walkthrough: http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
演练:http: //www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
回答by Ben Johnson
You can do this really easily with ack:
你可以很容易地用ack做到这一点:
ack -l 'cats' | ack -xl 'dogs'
-l
: return a list of files-x
: take the files from STDIN (the previous search) and only search those files
-l
: 返回文件列表-x
: 从 STDIN 中获取文件(之前的搜索)并且只搜索那些文件
And you can just keep piping until you get just the files you want.
你可以继续管道直到你得到你想要的文件。
回答by Sarath Chandra
This searches multiple words in multiple files:
这将在多个文件中搜索多个单词:
egrep 'abc|xyz' file1 file2 ..filen
回答by kurumi
awk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}END{ if (a && b && c) print "0" }'
you can then catch the return value with the shell
然后你可以用shell捕获返回值
if you have Ruby(1.9+)
如果你有 Ruby(1.9+)
ruby -0777 -ne 'print if /Dansk/ and /Norsk/ and /Svenka/' file
回答by Paused until further notice.
This is a blending of glenn Hymanman's and kurumi's answers which allows an arbitrary number of regexes instead of an arbitrary number of fixed words or a fixed set of regexes.
这是 glenn Hymanman 和 kurumi 的答案的混合,它允许使用任意数量的正则表达式,而不是任意数量的固定单词或一组固定的正则表达式。
#!/usr/bin/awk -f
# by Dennis Williamson - 2011-01-25
BEGIN {
for (i=ARGC-2; i>=1; i--) {
patterns[ARGV[i]] = 0;
delete ARGV[i];
}
}
{
for (p in patterns)
if (./multigrep.awk Dansk Norsk Svenska 'Language: .. - A.*c' dvdfile.dat
~ p)
matches[p] = 1
# print # the matching line could be printed
}
END {
for (p in patterns) {
if (matches[p] != 1)
exit 1
}
}
Run it like this:
像这样运行它:
find . -path '*/.svn' -prune -o -type f -exec gawk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}END{ if (a && b && c) print FILENAME }' {} \;
./path/to/file1.sh
./another/path/to/file2.txt
./blah/foo.php
回答by Nick Henry
Here's what worked well for me:
以下是对我有用的方法:
find . -path '*/.svn' -prune -o -type f -name "*.sh" -exec gawk '/Dansk/{a=1}/Norsk/{b=1}/Svenska/{c=1}END{ if (a && b && c) print FILENAME }' {} \;
./path/to/file1.sh
If I just wanted to find .sh files with these three, then I could have used:
如果我只想找到这三个的 .sh 文件,那么我可以使用:
##代码##