1 command-line grep text-processing
我在多个目录中有多个文件,我需要一个 grep 命令,该命令仅当文件中存在两种模式时才能返回输出。图案类似于AccessToken
和Registrationrequest
。图案不在同一条线上。AccessToken
可以在一行中,Registrationrequest
也可以在另一行中。还要在所有目录中的所有文件中递归搜索相同的内容。
试过
grep -r "string1” /directory/file | grep "string 2”
grep -rl 'string 1' | xargs grep 'string2' -l /directory/ file
grep -e string1 -e string2
Run Code Online (Sandbox Code Playgroud)
没有任何效果
有人可以帮忙吗?
当文件匹配搜索条件时,可以打印以下命令:
grep -Zril 'String1' | xargs -0 grep -il 'String2'
Run Code Online (Sandbox Code Playgroud)
下面是一个例子:
~/tmp$ ls
Dir1 Dir2 File1 File2
cat File1
AccessToken is not here
Registrationrequest it is here
cat File2
iAccess
ByteMe Registrationrequest
Run Code Online (Sandbox Code Playgroud)
我将File1
和都复制File2
到Dir1
和 中Dir2
进行测试:
~/tmp$ grep -Zril 'AccessToken' | xargs -0 grep -il 'Registrationrequest'
File1
Dir2/File1
Dir1/File1
Run Code Online (Sandbox Code Playgroud)
然后,如果您想查看文件中的内容,请将以下内容添加到搜索的末尾:
xargs grep -E "AccessToken|Registrationrequest"
Run Code Online (Sandbox Code Playgroud)
例子:
~/tmp$ grep -Zril 'AccessToken' | xargs -0 grep -il 'Registrationrequest' | xargs grep -E "AccessToken|Registrationrequest"
File1:AccessToken is not here
File1:Registrationrequest it is here
Dir2/File1:AccessToken is not here
Dir2/File1:Registrationrequest it is here
Dir1/File1:AccessToken is not here
Dir1/File1:Registrationrequest it is here
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!