我想匹配包含“John”和“Riya”和“bug”的模式。
示例_1:
//This file should match.
Hi John, How are you?
Riya, what is your age?
Fix this bug by end of the week.
示例_2:
//This file should not match.
Hi John, How are you?
Mike, what is your age?
Fix this bug by end of the week.
答案1
前瞻就是为这些而设计的。在 PCRE 模式-P
和 slurp 模式下调用 grep -z
。然后列出满足 3 个先行条件的输入文件。这(?s:.....)
将使点.
跨越换行符。
$ grep -Plz '(?s:(?=.*John)(?=.*Riya)(?=.*bug))' file
$ grep -Plzr '(?s:.....)' .
会递归列出当前目录下匹配的文件。
答案2
您可以按顺序使用find
多个来-exec grep
匹配每个模式:
find . -type f -exec grep -q "John" {} \; -exec grep -q "Riya" {} \; -print
这并不像看起来那么糟糕,因为find
它将在第一次失败的测试时停止。
答案3
我喜欢 Rakesh Sharma 的解决方案,grep 和 awk 非常有用。然而,远远超出 grep 范围的匹配可以通过专门的代码来处理,例如以下代码片段中的代码:
# Utility functions: print-as-echo, print-line-with-visual-space.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
pl " Input data file $FILE:"
head $FILE
pl " Results for glark, looking for \"John\" and \"Riya\" and \"bug\":"
# glark --no-filter -a -1 John -a -1 Riya bug --end-of-and $FILE
# glark --explain -a -1 John -a -1 Riya bug --end-of-and $FILE
glark -l -a -1 John -a -1 Riya bug --end-of-and $FILE
pe " Results for glark, inverse, expecting data2:"
glark -L -a -1 John -a -1 Riya bug --end-of-and $FILE
pl " Results for greple, looking for \"John\" and \"Riya\" and \"bug\":"
greple -l --block='(?s).*' 'John Riya bug' $FILE
pl " Results for rapgrep, looking for \"John\" and \"Riya\" and \"bug\":"
rapgrep -e=John -e=Riya -e=bug $FILE
pe " Results for rapgrep, reverse, expecting data2:"
rapgrep --reverse -e=John -e=Riya -e=bug $FILE
这将产生:
-----
Input data file data[12]:
==> data1 <==
//This file should match.
Hi John, How are you?
Riya, what is your age?
Fix this bug by end of the week.
==> data2 <==
//This file should not match.
Hi John, How are you?
Mike, what is your age?
Fix this bug by end of the week.
-----
Results for glark, looking for "John" and "Riya" and "bug":
data1
Results for glark, inverse, expecting data2:
data2
-----
Results for greple, looking for "John" and "Riya" and "bug":
data1
-----
Results for rapgrep, looking for "John" and "Riya" and "bug":
data1
Results for rapgrep, reverse, expecting data2:
data2
前两个具有 grep 之外的功能,但您将用功能来换取速度。在我们的商店,我们决定生产第三个项目 rapgrep,这是一个类似 grep 的代码,可以执行特定的功能。通过 glark 部分中注释掉的行,您可以看到 glark 工作原理的一些详细信息。
如果您有兴趣获得前两个,请参阅以下详细信息:
glark Search text files for complex regular expressions, grep (local man). (doc)
Path : /usr/local/bin/glark
Version : 1.10.5
Length : 23 lines
Type : Ruby script, ASCII text executable
Shebang : #!/usr/bin/ruby2.1
Home : https://github.com/jpace/glark (doc)
( But possibly better installed as "gem install glark", after
( installing ruby. Can also recurse.
greple grep with multiple keywords (man)
Path : ~/bin/greple
Version : - ( local: RepRev =, ~/bin/greple, 2017-07-02 )
Length : 2390 lines
Type : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
Home : https://github.com/kaz-utashiro/greple (doc)
rapgrep Require all patterns grep. (what)
Path : ~/bin/rapgrep
Version : 1.2
Length : 307 lines
Type : Perl script, ASCII text executable
Shebang : #!/usr/bin/perl
最美好的祝愿...干杯,drl
答案4
考虑 2 个文件example_1.txt
和example_2.txt
:
find ./ -type f -name "*1.txt" -exec grep -E "John|Riya|bug" {} \;
如果通配符不够,您可以使用-regex
find 中的选项