How to grep for two patterns in multiple files

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








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?







share|improve this question


























    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?







    share|improve this question
























      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?







      share|improve this question














      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?









      share|improve this question













      share|improve this question




      share|improve this question








      edited May 15 at 19:00









      dessert

      19.8k55594




      19.8k55594










      asked Apr 19 at 2:39









      user1573232

      132




      132




















          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 and r 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.





          share|improve this answer






















          • @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

















          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!






          share|improve this answer



























            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'





            share|improve this answer





























              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)





              share|improve this answer






















                Your Answer







                StackExchange.ready(function()
                var channelOptions =
                tags: "".split(" "),
                id: "89"
                ;
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function()
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled)
                StackExchange.using("snippets", function()
                createEditor();
                );

                else
                createEditor();

                );

                function createEditor()
                StackExchange.prepareEditor(
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: false,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                );



                );













                 

                draft saved


                draft discarded


















                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






























                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 and r 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.





                share|improve this answer






















                • @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














                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 and r 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.





                share|improve this answer






















                • @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












                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 and r 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.





                share|improve this answer














                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 and r 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.






                share|improve this answer














                share|improve this answer



                share|improve this answer








                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
















                • @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












                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!






                share|improve this answer
























                  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!






                  share|improve this answer






















                    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!






                    share|improve this answer












                    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!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 19 at 3:44









                    Terrance

                    17.3k23784




                    17.3k23784




















                        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'





                        share|improve this answer


























                          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'





                          share|improve this answer
























                            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'





                            share|improve this answer














                            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'






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Apr 19 at 23:26

























                            answered Apr 19 at 23:10









                            JJoao

                            1,24059




                            1,24059




















                                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)





                                share|improve this answer


























                                  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)





                                  share|improve this answer
























                                    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)





                                    share|improve this answer















                                    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)






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited May 15 at 18:59









                                    dessert

                                    19.8k55594




                                    19.8k55594










                                    answered Apr 19 at 23:41









                                    wjandrea

                                    7,16342255




                                    7,16342255



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        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













































































                                        Popular posts from this blog

                                        pylint3 and pip3 broken

                                        Missing snmpget and snmpwalk

                                        How to enroll fingerprints to Ubuntu 17.10 with VFS491