How to grep for two patterns in multiple files
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgO9GURib1T8z7lCwjOGLQaGtrueEthgQ8LO42ZX8cOfTqDK4jvDDpKkLFwf2J49kYCMNW7d4ABih_XCb_2UXdq5fPJDkoyg7-8g_YfRUot-XnaXkNYycsNp7lA5_TW9td0FFpLQ2APzKcZ/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYQ0N5W1qAOxLP7t7iOM6O6AzbZnkXUy16s7P_CWfOb5UbTQY_aDsc727chyphenhyphen5W4IppVNernMMQeaUFTB_rFzAd95_CDt-tnwN-nBx6JyUp2duGjPaL5-VgNO41AVsA_vu30EJcipdDG409/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
up vote
2
down vote
favorite
I have multiple files in multiple directories, and I need a grep command which can return the output only when both the patterns are present in the file. The patterns are like AccessToken
and Registrationrequest
. The patterns are not in the same line. AccessToken
can be in one line and Registrationrequest
can be in another line. Also search the same recursively across all files in all directories.
Tried
grep -r "string1â /directory/file | grep "string 2âÂÂ
grep -rl 'string 1' | xargs grep 'string2' -l /directory/ file
grep -e string1 -e string2
Nothing works
Can anyone please help?
command-line text-processing grep
add a comment |Â
up vote
2
down vote
favorite
I have multiple files in multiple directories, and I need a grep command which can return the output only when both the patterns are present in the file. The patterns are like AccessToken
and Registrationrequest
. The patterns are not in the same line. AccessToken
can be in one line and Registrationrequest
can be in another line. Also search the same recursively across all files in all directories.
Tried
grep -r "string1â /directory/file | grep "string 2âÂÂ
grep -rl 'string 1' | xargs grep 'string2' -l /directory/ file
grep -e string1 -e string2
Nothing works
Can anyone please help?
command-line text-processing grep
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have multiple files in multiple directories, and I need a grep command which can return the output only when both the patterns are present in the file. The patterns are like AccessToken
and Registrationrequest
. The patterns are not in the same line. AccessToken
can be in one line and Registrationrequest
can be in another line. Also search the same recursively across all files in all directories.
Tried
grep -r "string1â /directory/file | grep "string 2âÂÂ
grep -rl 'string 1' | xargs grep 'string2' -l /directory/ file
grep -e string1 -e string2
Nothing works
Can anyone please help?
command-line text-processing grep
I have multiple files in multiple directories, and I need a grep command which can return the output only when both the patterns are present in the file. The patterns are like AccessToken
and Registrationrequest
. The patterns are not in the same line. AccessToken
can be in one line and Registrationrequest
can be in another line. Also search the same recursively across all files in all directories.
Tried
grep -r "string1â /directory/file | grep "string 2âÂÂ
grep -rl 'string 1' | xargs grep 'string2' -l /directory/ file
grep -e string1 -e string2
Nothing works
Can anyone please help?
command-line text-processing grep
edited May 15 at 19:00
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
dessert
19.8k55594
19.8k55594
asked Apr 19 at 2:39
user1573232
132
132
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
4
down vote
accepted
Just in case the files are very large and making two passes on them is expensive and you want just the filenames, using find
+awk
:
find . -type f -exec awk 'FNR == 1 a=0; r=0 /AccessToken/a=1 /Registrationrequest/r=1 a && r print FILENAME; nextfile' +
- we set two flag variables
a
andr
for when the corresponding patterns were found, cleared at the start of each file (FNR == 1
) - when both variables are true, we print the filename and move on to the next file.
@muru can help with syntax of saving the filename to a file instead of printing it
â user1573232
Apr 19 at 5:36
@user1573232 just put a> output-file
at the end of the command
â muru
Apr 19 at 5:41
add a comment |Â
up vote
6
down vote
The following command can print out when the file matches the search criteria:
grep -Zril 'String1' | xargs -0 grep -il 'String2'
Here is an example:
~/tmp$ ls
Dir1 Dir2 File1 File2
cat File1
AccessToken is not here
Registrationrequest it is here
cat File2
iAccess
ByteMe Registrationrequest
I copied both File1
and File2
into the Dir1
and Dir2
for testing:
~/tmp$ grep -Zril 'AccessToken' | xargs -0 grep -il 'Registrationrequest'
File1
Dir2/File1
Dir1/File1
Then if you want to see what is in the files add the following to the end of the search:
xargs grep -E "AccessToken|Registrationrequest"
Example:
~/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
Hope this helps!
add a comment |Â
up vote
2
down vote
grep -Erzl 'STR1.*STR2|STR2.*STR1'
where option -z
ends up slurping the files as a single line.
More precisely:
grep -Erzl 'AccessToken.*Registrationrequest|Registrationrequest.*AccessToken'
add a comment |Â
up vote
1
down vote
while
loop
This may not be as clever or resource-effective as other solutions, but it works:
while read -rd '' filename; do
if grep -q "AccessToken" "$filename" &&
grep -q "Registrationrequest" "$filename"
then
echo "$filename"
fi
done < <(find . -type f -print0)
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Just in case the files are very large and making two passes on them is expensive and you want just the filenames, using find
+awk
:
find . -type f -exec awk 'FNR == 1 a=0; r=0 /AccessToken/a=1 /Registrationrequest/r=1 a && r print FILENAME; nextfile' +
- we set two flag variables
a
andr
for when the corresponding patterns were found, cleared at the start of each file (FNR == 1
) - when both variables are true, we print the filename and move on to the next file.
@muru can help with syntax of saving the filename to a file instead of printing it
â user1573232
Apr 19 at 5:36
@user1573232 just put a> output-file
at the end of the command
â muru
Apr 19 at 5:41
add a comment |Â
up vote
4
down vote
accepted
Just in case the files are very large and making two passes on them is expensive and you want just the filenames, using find
+awk
:
find . -type f -exec awk 'FNR == 1 a=0; r=0 /AccessToken/a=1 /Registrationrequest/r=1 a && r print FILENAME; nextfile' +
- we set two flag variables
a
andr
for when the corresponding patterns were found, cleared at the start of each file (FNR == 1
) - when both variables are true, we print the filename and move on to the next file.
@muru can help with syntax of saving the filename to a file instead of printing it
â user1573232
Apr 19 at 5:36
@user1573232 just put a> output-file
at the end of the command
â muru
Apr 19 at 5:41
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Just in case the files are very large and making two passes on them is expensive and you want just the filenames, using find
+awk
:
find . -type f -exec awk 'FNR == 1 a=0; r=0 /AccessToken/a=1 /Registrationrequest/r=1 a && r print FILENAME; nextfile' +
- we set two flag variables
a
andr
for when the corresponding patterns were found, cleared at the start of each file (FNR == 1
) - when both variables are true, we print the filename and move on to the next file.
Just in case the files are very large and making two passes on them is expensive and you want just the filenames, using find
+awk
:
find . -type f -exec awk 'FNR == 1 a=0; r=0 /AccessToken/a=1 /Registrationrequest/r=1 a && r print FILENAME; nextfile' +
- we set two flag variables
a
andr
for when the corresponding patterns were found, cleared at the start of each file (FNR == 1
) - when both variables are true, we print the filename and move on to the next file.
edited Apr 19 at 4:46
answered Apr 19 at 4:13
muru
129k19272462
129k19272462
@muru can help with syntax of saving the filename to a file instead of printing it
â user1573232
Apr 19 at 5:36
@user1573232 just put a> output-file
at the end of the command
â muru
Apr 19 at 5:41
add a comment |Â
@muru can help with syntax of saving the filename to a file instead of printing it
â user1573232
Apr 19 at 5:36
@user1573232 just put a> output-file
at the end of the command
â muru
Apr 19 at 5:41
@muru can help with syntax of saving the filename to a file instead of printing it
â user1573232
Apr 19 at 5:36
@muru can help with syntax of saving the filename to a file instead of printing it
â user1573232
Apr 19 at 5:36
@user1573232 just put a
> output-file
at the end of the commandâ muru
Apr 19 at 5:41
@user1573232 just put a
> output-file
at the end of the commandâ muru
Apr 19 at 5:41
add a comment |Â
up vote
6
down vote
The following command can print out when the file matches the search criteria:
grep -Zril 'String1' | xargs -0 grep -il 'String2'
Here is an example:
~/tmp$ ls
Dir1 Dir2 File1 File2
cat File1
AccessToken is not here
Registrationrequest it is here
cat File2
iAccess
ByteMe Registrationrequest
I copied both File1
and File2
into the Dir1
and Dir2
for testing:
~/tmp$ grep -Zril 'AccessToken' | xargs -0 grep -il 'Registrationrequest'
File1
Dir2/File1
Dir1/File1
Then if you want to see what is in the files add the following to the end of the search:
xargs grep -E "AccessToken|Registrationrequest"
Example:
~/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
Hope this helps!
add a comment |Â
up vote
6
down vote
The following command can print out when the file matches the search criteria:
grep -Zril 'String1' | xargs -0 grep -il 'String2'
Here is an example:
~/tmp$ ls
Dir1 Dir2 File1 File2
cat File1
AccessToken is not here
Registrationrequest it is here
cat File2
iAccess
ByteMe Registrationrequest
I copied both File1
and File2
into the Dir1
and Dir2
for testing:
~/tmp$ grep -Zril 'AccessToken' | xargs -0 grep -il 'Registrationrequest'
File1
Dir2/File1
Dir1/File1
Then if you want to see what is in the files add the following to the end of the search:
xargs grep -E "AccessToken|Registrationrequest"
Example:
~/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
Hope this helps!
add a comment |Â
up vote
6
down vote
up vote
6
down vote
The following command can print out when the file matches the search criteria:
grep -Zril 'String1' | xargs -0 grep -il 'String2'
Here is an example:
~/tmp$ ls
Dir1 Dir2 File1 File2
cat File1
AccessToken is not here
Registrationrequest it is here
cat File2
iAccess
ByteMe Registrationrequest
I copied both File1
and File2
into the Dir1
and Dir2
for testing:
~/tmp$ grep -Zril 'AccessToken' | xargs -0 grep -il 'Registrationrequest'
File1
Dir2/File1
Dir1/File1
Then if you want to see what is in the files add the following to the end of the search:
xargs grep -E "AccessToken|Registrationrequest"
Example:
~/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
Hope this helps!
The following command can print out when the file matches the search criteria:
grep -Zril 'String1' | xargs -0 grep -il 'String2'
Here is an example:
~/tmp$ ls
Dir1 Dir2 File1 File2
cat File1
AccessToken is not here
Registrationrequest it is here
cat File2
iAccess
ByteMe Registrationrequest
I copied both File1
and File2
into the Dir1
and Dir2
for testing:
~/tmp$ grep -Zril 'AccessToken' | xargs -0 grep -il 'Registrationrequest'
File1
Dir2/File1
Dir1/File1
Then if you want to see what is in the files add the following to the end of the search:
xargs grep -E "AccessToken|Registrationrequest"
Example:
~/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
Hope this helps!
answered Apr 19 at 3:44
![](https://i.stack.imgur.com/1hPwN.jpg?s=32&g=1)
![](https://i.stack.imgur.com/1hPwN.jpg?s=32&g=1)
Terrance
17.3k23784
17.3k23784
add a comment |Â
add a comment |Â
up vote
2
down vote
grep -Erzl 'STR1.*STR2|STR2.*STR1'
where option -z
ends up slurping the files as a single line.
More precisely:
grep -Erzl 'AccessToken.*Registrationrequest|Registrationrequest.*AccessToken'
add a comment |Â
up vote
2
down vote
grep -Erzl 'STR1.*STR2|STR2.*STR1'
where option -z
ends up slurping the files as a single line.
More precisely:
grep -Erzl 'AccessToken.*Registrationrequest|Registrationrequest.*AccessToken'
add a comment |Â
up vote
2
down vote
up vote
2
down vote
grep -Erzl 'STR1.*STR2|STR2.*STR1'
where option -z
ends up slurping the files as a single line.
More precisely:
grep -Erzl 'AccessToken.*Registrationrequest|Registrationrequest.*AccessToken'
grep -Erzl 'STR1.*STR2|STR2.*STR1'
where option -z
ends up slurping the files as a single line.
More precisely:
grep -Erzl 'AccessToken.*Registrationrequest|Registrationrequest.*AccessToken'
edited Apr 19 at 23:26
answered Apr 19 at 23:10
JJoao
1,24059
1,24059
add a comment |Â
add a comment |Â
up vote
1
down vote
while
loop
This may not be as clever or resource-effective as other solutions, but it works:
while read -rd '' filename; do
if grep -q "AccessToken" "$filename" &&
grep -q "Registrationrequest" "$filename"
then
echo "$filename"
fi
done < <(find . -type f -print0)
add a comment |Â
up vote
1
down vote
while
loop
This may not be as clever or resource-effective as other solutions, but it works:
while read -rd '' filename; do
if grep -q "AccessToken" "$filename" &&
grep -q "Registrationrequest" "$filename"
then
echo "$filename"
fi
done < <(find . -type f -print0)
add a comment |Â
up vote
1
down vote
up vote
1
down vote
while
loop
This may not be as clever or resource-effective as other solutions, but it works:
while read -rd '' filename; do
if grep -q "AccessToken" "$filename" &&
grep -q "Registrationrequest" "$filename"
then
echo "$filename"
fi
done < <(find . -type f -print0)
while
loop
This may not be as clever or resource-effective as other solutions, but it works:
while read -rd '' filename; do
if grep -q "AccessToken" "$filename" &&
grep -q "Registrationrequest" "$filename"
then
echo "$filename"
fi
done < <(find . -type f -print0)
edited May 15 at 18:59
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
![](https://i.stack.imgur.com/9L8vd.png?s=32&g=1)
dessert
19.8k55594
19.8k55594
answered Apr 19 at 23:41
![](https://i.stack.imgur.com/eVuAv.png?s=32&g=1)
![](https://i.stack.imgur.com/eVuAv.png?s=32&g=1)
wjandrea
7,16342255
7,16342255
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1026259%2fhow-to-grep-for-two-patterns-in-multiple-files%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password