Search for two sequential patterns with any number of characters in between using grep [duplicate]

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











up vote
0
down vote

favorite













This question already has an answer here:



  • Match two words that is on the same line

    4 answers



I want to look for lines with "word1 ... word2" where '...' could be any different characters. So far I have used two greps for the same like this:



grep "$word1" $filename | grep "$word2"



Is there a faster way to do this by suppose something like this:



grep "$word1*$word2" $filename



where maybe * could be some special character which can be any other character(s)?







share|improve this question













marked as duplicate by Jeff Schaller, schily, slm♦ Jun 22 at 1:11


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















    up vote
    0
    down vote

    favorite













    This question already has an answer here:



    • Match two words that is on the same line

      4 answers



    I want to look for lines with "word1 ... word2" where '...' could be any different characters. So far I have used two greps for the same like this:



    grep "$word1" $filename | grep "$word2"



    Is there a faster way to do this by suppose something like this:



    grep "$word1*$word2" $filename



    where maybe * could be some special character which can be any other character(s)?







    share|improve this question













    marked as duplicate by Jeff Schaller, schily, slm♦ Jun 22 at 1:11


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
















      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite












      This question already has an answer here:



      • Match two words that is on the same line

        4 answers



      I want to look for lines with "word1 ... word2" where '...' could be any different characters. So far I have used two greps for the same like this:



      grep "$word1" $filename | grep "$word2"



      Is there a faster way to do this by suppose something like this:



      grep "$word1*$word2" $filename



      where maybe * could be some special character which can be any other character(s)?







      share|improve this question














      This question already has an answer here:



      • Match two words that is on the same line

        4 answers



      I want to look for lines with "word1 ... word2" where '...' could be any different characters. So far I have used two greps for the same like this:



      grep "$word1" $filename | grep "$word2"



      Is there a faster way to do this by suppose something like this:



      grep "$word1*$word2" $filename



      where maybe * could be some special character which can be any other character(s)?





      This question already has an answer here:



      • Match two words that is on the same line

        4 answers









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 21 at 11:39









      Jeff Schaller

      30.8k846104




      30.8k846104









      asked Jun 21 at 11:39









      Pratik Mayekar

      1077




      1077




      marked as duplicate by Jeff Schaller, schily, slm♦ Jun 22 at 1:11


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Jeff Schaller, schily, slm♦ Jun 22 at 1:11


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Use .*:



          grep "$word1.*$word2" "$filename"



          • . matches any character


          • * matches any number of the preceding character





          share|improve this answer



















          • 2




            More precisely, .* matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
            – NickD
            Jun 21 at 11:50










          • This is precisely what I was looking for
            – Pratik Mayekar
            Jun 21 at 11:52

















          up vote
          0
          down vote













          If you need the two words to be delimited, i.e. if you do not want to match abba if one of the words is bb, then use



          grep "\<$word1\>.*\<$word2\>" "$filename"


          The pattern < (here \< to escape the first backslash from the shell) matches just before a word, and > works similarly but just after a word.



          There's alo b that matches either before and after, and [[:<:]] and [[:>:]] that work just like < and >. Which ones that are implemented by grep varies.






          share|improve this answer




























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Use .*:



            grep "$word1.*$word2" "$filename"



            • . matches any character


            • * matches any number of the preceding character





            share|improve this answer



















            • 2




              More precisely, .* matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
              – NickD
              Jun 21 at 11:50










            • This is precisely what I was looking for
              – Pratik Mayekar
              Jun 21 at 11:52














            up vote
            2
            down vote



            accepted










            Use .*:



            grep "$word1.*$word2" "$filename"



            • . matches any character


            • * matches any number of the preceding character





            share|improve this answer



















            • 2




              More precisely, .* matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
              – NickD
              Jun 21 at 11:50










            • This is precisely what I was looking for
              – Pratik Mayekar
              Jun 21 at 11:52












            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            Use .*:



            grep "$word1.*$word2" "$filename"



            • . matches any character


            • * matches any number of the preceding character





            share|improve this answer















            Use .*:



            grep "$word1.*$word2" "$filename"



            • . matches any character


            • * matches any number of the preceding character






            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Jun 21 at 11:53


























            answered Jun 21 at 11:43









            RoVo

            83219




            83219







            • 2




              More precisely, .* matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
              – NickD
              Jun 21 at 11:50










            • This is precisely what I was looking for
              – Pratik Mayekar
              Jun 21 at 11:52












            • 2




              More precisely, .* matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
              – NickD
              Jun 21 at 11:50










            • This is precisely what I was looking for
              – Pratik Mayekar
              Jun 21 at 11:52







            2




            2




            More precisely, .* matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
            – NickD
            Jun 21 at 11:50




            More precisely, .* matches any chararacter repeated 0 or more times (IOW, it matches any string, including the empty string).
            – NickD
            Jun 21 at 11:50












            This is precisely what I was looking for
            – Pratik Mayekar
            Jun 21 at 11:52




            This is precisely what I was looking for
            – Pratik Mayekar
            Jun 21 at 11:52












            up vote
            0
            down vote













            If you need the two words to be delimited, i.e. if you do not want to match abba if one of the words is bb, then use



            grep "\<$word1\>.*\<$word2\>" "$filename"


            The pattern < (here \< to escape the first backslash from the shell) matches just before a word, and > works similarly but just after a word.



            There's alo b that matches either before and after, and [[:<:]] and [[:>:]] that work just like < and >. Which ones that are implemented by grep varies.






            share|improve this answer

























              up vote
              0
              down vote













              If you need the two words to be delimited, i.e. if you do not want to match abba if one of the words is bb, then use



              grep "\<$word1\>.*\<$word2\>" "$filename"


              The pattern < (here \< to escape the first backslash from the shell) matches just before a word, and > works similarly but just after a word.



              There's alo b that matches either before and after, and [[:<:]] and [[:>:]] that work just like < and >. Which ones that are implemented by grep varies.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                If you need the two words to be delimited, i.e. if you do not want to match abba if one of the words is bb, then use



                grep "\<$word1\>.*\<$word2\>" "$filename"


                The pattern < (here \< to escape the first backslash from the shell) matches just before a word, and > works similarly but just after a word.



                There's alo b that matches either before and after, and [[:<:]] and [[:>:]] that work just like < and >. Which ones that are implemented by grep varies.






                share|improve this answer













                If you need the two words to be delimited, i.e. if you do not want to match abba if one of the words is bb, then use



                grep "\<$word1\>.*\<$word2\>" "$filename"


                The pattern < (here \< to escape the first backslash from the shell) matches just before a word, and > works similarly but just after a word.



                There's alo b that matches either before and after, and [[:<:]] and [[:>:]] that work just like < and >. Which ones that are implemented by grep varies.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jun 21 at 12:03









                Kusalananda

                101k13199312




                101k13199312












                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay