How to run grep with multiple AND patterns?

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











up vote
75
down vote

favorite
28












I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence:



grep pattern1 | grep pattern2 | ...


So how to convert it to something like?



grep pattern1 & pattern2 & pattern3


I would like to use single grep because I am building arguments dynamically, so everything has to fit in one string. Using filter is system feature, not grep, so it is not an argument for it.




Don't confuse this question with:



grep "pattern1|pattern2|..."


This is an OR multi pattern match.







share|improve this question





















  • Similar: Match all patterns from file at once
    – kenorb
    Dec 22 '16 at 15:26










  • Similar question on SO: Check if multiple strings or regexes exist in a file
    – codeforester
    Apr 13 at 17:47














up vote
75
down vote

favorite
28












I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence:



grep pattern1 | grep pattern2 | ...


So how to convert it to something like?



grep pattern1 & pattern2 & pattern3


I would like to use single grep because I am building arguments dynamically, so everything has to fit in one string. Using filter is system feature, not grep, so it is not an argument for it.




Don't confuse this question with:



grep "pattern1|pattern2|..."


This is an OR multi pattern match.







share|improve this question





















  • Similar: Match all patterns from file at once
    – kenorb
    Dec 22 '16 at 15:26










  • Similar question on SO: Check if multiple strings or regexes exist in a file
    – codeforester
    Apr 13 at 17:47












up vote
75
down vote

favorite
28









up vote
75
down vote

favorite
28






28





I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence:



grep pattern1 | grep pattern2 | ...


So how to convert it to something like?



grep pattern1 & pattern2 & pattern3


I would like to use single grep because I am building arguments dynamically, so everything has to fit in one string. Using filter is system feature, not grep, so it is not an argument for it.




Don't confuse this question with:



grep "pattern1|pattern2|..."


This is an OR multi pattern match.







share|improve this question













I would like to get the multi pattern match with implicit AND between patterns, i.e. equivalent to running several greps in a sequence:



grep pattern1 | grep pattern2 | ...


So how to convert it to something like?



grep pattern1 & pattern2 & pattern3


I would like to use single grep because I am building arguments dynamically, so everything has to fit in one string. Using filter is system feature, not grep, so it is not an argument for it.




Don't confuse this question with:



grep "pattern1|pattern2|..."


This is an OR multi pattern match.









share|improve this question












share|improve this question




share|improve this question








edited Nov 13 '12 at 21:01









dubiousjim

1,9481223




1,9481223









asked Nov 10 '12 at 7:45









greenoldman

2,438103958




2,438103958











  • Similar: Match all patterns from file at once
    – kenorb
    Dec 22 '16 at 15:26










  • Similar question on SO: Check if multiple strings or regexes exist in a file
    – codeforester
    Apr 13 at 17:47
















  • Similar: Match all patterns from file at once
    – kenorb
    Dec 22 '16 at 15:26










  • Similar question on SO: Check if multiple strings or regexes exist in a file
    – codeforester
    Apr 13 at 17:47















Similar: Match all patterns from file at once
– kenorb
Dec 22 '16 at 15:26




Similar: Match all patterns from file at once
– kenorb
Dec 22 '16 at 15:26












Similar question on SO: Check if multiple strings or regexes exist in a file
– codeforester
Apr 13 at 17:47




Similar question on SO: Check if multiple strings or regexes exist in a file
– codeforester
Apr 13 at 17:47










7 Answers
7






active

oldest

votes

















up vote
65
down vote



accepted










agrep can do it with this syntax:



agrep 'pattern1;pattern2'


With GNU grep, when built with PCRE support, you can do:



grep -P '^(?=.*pattern1)(?=.*pattern2)'


With ast grep:



grep -X '.*pattern1.*&.*pattern2.*'


(adding .*s as <x>&<y> matches strings that match both <x> and <y> exactly, a&b would never match as there's no such string that can be both a and b at the same time).



If the patterns don't overlap, you may also be able to do:



grep -e 'pattern1.*pattern2' -e 'pattern2.*pattern1'


The best portable way is probably with awk as already mentioned:



awk '/pattern1/ && /pattern2/'


With sed:



sed -e '/pattern1/!d' -e '/pattern2/!d'


Please beware that all those will have different regular expression syntax.






share|improve this answer



















  • 1




    The agrep syntax is not working for me... which version was it introduced in?
    – Raman
    Sep 5 '16 at 22:15










  • @Raman 2.04 from 1992 already had it. I've no reason to believe it wasn't there from the start. Newer (after 1992) versions of agrep can be found included with glimpse/webglimpse. Possibly you have a different implementation. I had a mistake for the ast-grep version though, the option for augmented regexps is -X, not -A.
    – Stéphane Chazelas
    Sep 6 '16 at 5:55











  • @StéphaneChazelas Thanks, I have agrep 0.8.0 on Fedora 23. This appears to be a different agrep than the one you reference.
    – Raman
    Sep 6 '16 at 6:37






  • 1




    @Raman, yours sounds like TRE agrep.
    – Stéphane Chazelas
    Sep 6 '16 at 7:01






  • 1




    @Techiee, or just awk '/p1/ && /p2/ n++; END print 0+n'
    – Stéphane Chazelas
    Jun 28 '17 at 20:23

















up vote
16
down vote













You didn't specify grep version, this is important. Some regexp engines allow multiple matching groupped by AND using '&' but this is non-standard and non-portable feature. But, at least GNU grep doesn't support this.



OTOH you can simply replace grep with sed, awk, perl, etc. (listed in order of weight increasing). With awk, the command would look like




awk '/regexp1/ && /regexp2/ && /regexp3/ print; '


and it can be constructed to be specified in command line in easy way.






share|improve this answer

















  • 3




    Just remember that awk uses ERE's, e.g. the equivalent of grep -E, as opposed to the BRE's that plain grep uses.
    – jw013
    Nov 10 '12 at 9:42






  • 3




    awk's regexes are called EREs, but in fact they're a bit idiosyncratic. Here are probably more details than anyone cares for: wiki.alpinelinux.org/wiki/Regex
    – dubiousjim
    Nov 10 '12 at 15:35











  • Thank you, grep 2.7.3 (openSUSE). I upvoted you, but I will keep question open for a while, maybe there is some trick for grep (not that I dislike awk -- simply knowing more is better).
    – greenoldman
    Nov 10 '12 at 15:42







  • 1




    The default action is to print the matching line so the print; part isn't really necessary or useful here.
    – tripleee
    Apr 20 '17 at 11:58


















up vote
6
down vote













This is not a very good solution but illustrates a somewhat cool "trick"



function chained-grep() chained-grep "$@"


cat something | chained-grep all patterns must match order but matter dont





share|improve this answer




























    up vote
    6
    down vote



    +50












    If patterns.txt contains one pattern per line, you can do something like this:



    awk 'NR==FNRa[$0];nextfor(i in a)if($0!~i)next1' patterns.txt -


    To search for fixed strings instead of regular expressions, use this variant:



    awk 'NR==FNRa[$0];nextfor(i in a)if(!index($0,i))next1' patterns.txt -


    To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.






    share|improve this answer






























      up vote
      2
      down vote













      git grep



      Here is the syntax using git grep combining multiple patterns using Boolean expressions:



      git grep -e pattern1 --and -e pattern2 --and -e pattern3


      The above command will print lines matching all the patterns at once.



      If the files aren't under version control, add --no-index param.




      Search files in the current directory that is not managed by Git.




      Check man git-grep for help.



      See also: Check if all of multiple strings or regexes exist in a file.






      share|improve this answer






























        up vote
        2
        down vote













        This shell command may help:



        eval "</dev/stdin $(printf "|grep '%s'" pattern1 pattern2)" FILE


        However it's easier if all your patterns are stored in the file, so you can define the following alias:



        alias grep-all="cat $(xargs printf '| grep "%s"' < patterns.txt)"


        and use it as:



        cat text.txt | grep-all


        You can of course modify the alias depending on your required syntax, so with this alias:



        alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < patterns.txt)"


        you can use just a single command, e.g.



        grep-all text.txt


        For more ideas, check also: Match all patterns from file at once.



        For AND operation per file, see: Check if all of multiple strings or regexes exist in a file.






        share|improve this answer






























          up vote
          0
          down vote













          ripgrep



          Here is the example using rg:



          rg -N '(?P<p1>.*pattern1.*)(?P<p2>.*pattern2.*)(?P<p3>.*pattern3.*)' file.txt


          It's one of the quickest grepping tools, since it's built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.



          See also related feature request at GH-875.






          share|improve this answer























            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "106"
            ;
            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: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2funix.stackexchange.com%2fquestions%2f55359%2fhow-to-run-grep-with-multiple-and-patterns%23new-answer', 'question_page');

            );

            Post as a guest






























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            65
            down vote



            accepted










            agrep can do it with this syntax:



            agrep 'pattern1;pattern2'


            With GNU grep, when built with PCRE support, you can do:



            grep -P '^(?=.*pattern1)(?=.*pattern2)'


            With ast grep:



            grep -X '.*pattern1.*&.*pattern2.*'


            (adding .*s as <x>&<y> matches strings that match both <x> and <y> exactly, a&b would never match as there's no such string that can be both a and b at the same time).



            If the patterns don't overlap, you may also be able to do:



            grep -e 'pattern1.*pattern2' -e 'pattern2.*pattern1'


            The best portable way is probably with awk as already mentioned:



            awk '/pattern1/ && /pattern2/'


            With sed:



            sed -e '/pattern1/!d' -e '/pattern2/!d'


            Please beware that all those will have different regular expression syntax.






            share|improve this answer



















            • 1




              The agrep syntax is not working for me... which version was it introduced in?
              – Raman
              Sep 5 '16 at 22:15










            • @Raman 2.04 from 1992 already had it. I've no reason to believe it wasn't there from the start. Newer (after 1992) versions of agrep can be found included with glimpse/webglimpse. Possibly you have a different implementation. I had a mistake for the ast-grep version though, the option for augmented regexps is -X, not -A.
              – Stéphane Chazelas
              Sep 6 '16 at 5:55











            • @StéphaneChazelas Thanks, I have agrep 0.8.0 on Fedora 23. This appears to be a different agrep than the one you reference.
              – Raman
              Sep 6 '16 at 6:37






            • 1




              @Raman, yours sounds like TRE agrep.
              – Stéphane Chazelas
              Sep 6 '16 at 7:01






            • 1




              @Techiee, or just awk '/p1/ && /p2/ n++; END print 0+n'
              – Stéphane Chazelas
              Jun 28 '17 at 20:23














            up vote
            65
            down vote



            accepted










            agrep can do it with this syntax:



            agrep 'pattern1;pattern2'


            With GNU grep, when built with PCRE support, you can do:



            grep -P '^(?=.*pattern1)(?=.*pattern2)'


            With ast grep:



            grep -X '.*pattern1.*&.*pattern2.*'


            (adding .*s as <x>&<y> matches strings that match both <x> and <y> exactly, a&b would never match as there's no such string that can be both a and b at the same time).



            If the patterns don't overlap, you may also be able to do:



            grep -e 'pattern1.*pattern2' -e 'pattern2.*pattern1'


            The best portable way is probably with awk as already mentioned:



            awk '/pattern1/ && /pattern2/'


            With sed:



            sed -e '/pattern1/!d' -e '/pattern2/!d'


            Please beware that all those will have different regular expression syntax.






            share|improve this answer



















            • 1




              The agrep syntax is not working for me... which version was it introduced in?
              – Raman
              Sep 5 '16 at 22:15










            • @Raman 2.04 from 1992 already had it. I've no reason to believe it wasn't there from the start. Newer (after 1992) versions of agrep can be found included with glimpse/webglimpse. Possibly you have a different implementation. I had a mistake for the ast-grep version though, the option for augmented regexps is -X, not -A.
              – Stéphane Chazelas
              Sep 6 '16 at 5:55











            • @StéphaneChazelas Thanks, I have agrep 0.8.0 on Fedora 23. This appears to be a different agrep than the one you reference.
              – Raman
              Sep 6 '16 at 6:37






            • 1




              @Raman, yours sounds like TRE agrep.
              – Stéphane Chazelas
              Sep 6 '16 at 7:01






            • 1




              @Techiee, or just awk '/p1/ && /p2/ n++; END print 0+n'
              – Stéphane Chazelas
              Jun 28 '17 at 20:23












            up vote
            65
            down vote



            accepted







            up vote
            65
            down vote



            accepted






            agrep can do it with this syntax:



            agrep 'pattern1;pattern2'


            With GNU grep, when built with PCRE support, you can do:



            grep -P '^(?=.*pattern1)(?=.*pattern2)'


            With ast grep:



            grep -X '.*pattern1.*&.*pattern2.*'


            (adding .*s as <x>&<y> matches strings that match both <x> and <y> exactly, a&b would never match as there's no such string that can be both a and b at the same time).



            If the patterns don't overlap, you may also be able to do:



            grep -e 'pattern1.*pattern2' -e 'pattern2.*pattern1'


            The best portable way is probably with awk as already mentioned:



            awk '/pattern1/ && /pattern2/'


            With sed:



            sed -e '/pattern1/!d' -e '/pattern2/!d'


            Please beware that all those will have different regular expression syntax.






            share|improve this answer















            agrep can do it with this syntax:



            agrep 'pattern1;pattern2'


            With GNU grep, when built with PCRE support, you can do:



            grep -P '^(?=.*pattern1)(?=.*pattern2)'


            With ast grep:



            grep -X '.*pattern1.*&.*pattern2.*'


            (adding .*s as <x>&<y> matches strings that match both <x> and <y> exactly, a&b would never match as there's no such string that can be both a and b at the same time).



            If the patterns don't overlap, you may also be able to do:



            grep -e 'pattern1.*pattern2' -e 'pattern2.*pattern1'


            The best portable way is probably with awk as already mentioned:



            awk '/pattern1/ && /pattern2/'


            With sed:



            sed -e '/pattern1/!d' -e '/pattern2/!d'


            Please beware that all those will have different regular expression syntax.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Sep 6 '16 at 5:55


























            answered Nov 10 '12 at 20:13









            Stéphane Chazelas

            278k52513844




            278k52513844







            • 1




              The agrep syntax is not working for me... which version was it introduced in?
              – Raman
              Sep 5 '16 at 22:15










            • @Raman 2.04 from 1992 already had it. I've no reason to believe it wasn't there from the start. Newer (after 1992) versions of agrep can be found included with glimpse/webglimpse. Possibly you have a different implementation. I had a mistake for the ast-grep version though, the option for augmented regexps is -X, not -A.
              – Stéphane Chazelas
              Sep 6 '16 at 5:55











            • @StéphaneChazelas Thanks, I have agrep 0.8.0 on Fedora 23. This appears to be a different agrep than the one you reference.
              – Raman
              Sep 6 '16 at 6:37






            • 1




              @Raman, yours sounds like TRE agrep.
              – Stéphane Chazelas
              Sep 6 '16 at 7:01






            • 1




              @Techiee, or just awk '/p1/ && /p2/ n++; END print 0+n'
              – Stéphane Chazelas
              Jun 28 '17 at 20:23












            • 1




              The agrep syntax is not working for me... which version was it introduced in?
              – Raman
              Sep 5 '16 at 22:15










            • @Raman 2.04 from 1992 already had it. I've no reason to believe it wasn't there from the start. Newer (after 1992) versions of agrep can be found included with glimpse/webglimpse. Possibly you have a different implementation. I had a mistake for the ast-grep version though, the option for augmented regexps is -X, not -A.
              – Stéphane Chazelas
              Sep 6 '16 at 5:55











            • @StéphaneChazelas Thanks, I have agrep 0.8.0 on Fedora 23. This appears to be a different agrep than the one you reference.
              – Raman
              Sep 6 '16 at 6:37






            • 1




              @Raman, yours sounds like TRE agrep.
              – Stéphane Chazelas
              Sep 6 '16 at 7:01






            • 1




              @Techiee, or just awk '/p1/ && /p2/ n++; END print 0+n'
              – Stéphane Chazelas
              Jun 28 '17 at 20:23







            1




            1




            The agrep syntax is not working for me... which version was it introduced in?
            – Raman
            Sep 5 '16 at 22:15




            The agrep syntax is not working for me... which version was it introduced in?
            – Raman
            Sep 5 '16 at 22:15












            @Raman 2.04 from 1992 already had it. I've no reason to believe it wasn't there from the start. Newer (after 1992) versions of agrep can be found included with glimpse/webglimpse. Possibly you have a different implementation. I had a mistake for the ast-grep version though, the option for augmented regexps is -X, not -A.
            – Stéphane Chazelas
            Sep 6 '16 at 5:55





            @Raman 2.04 from 1992 already had it. I've no reason to believe it wasn't there from the start. Newer (after 1992) versions of agrep can be found included with glimpse/webglimpse. Possibly you have a different implementation. I had a mistake for the ast-grep version though, the option for augmented regexps is -X, not -A.
            – Stéphane Chazelas
            Sep 6 '16 at 5:55













            @StéphaneChazelas Thanks, I have agrep 0.8.0 on Fedora 23. This appears to be a different agrep than the one you reference.
            – Raman
            Sep 6 '16 at 6:37




            @StéphaneChazelas Thanks, I have agrep 0.8.0 on Fedora 23. This appears to be a different agrep than the one you reference.
            – Raman
            Sep 6 '16 at 6:37




            1




            1




            @Raman, yours sounds like TRE agrep.
            – Stéphane Chazelas
            Sep 6 '16 at 7:01




            @Raman, yours sounds like TRE agrep.
            – Stéphane Chazelas
            Sep 6 '16 at 7:01




            1




            1




            @Techiee, or just awk '/p1/ && /p2/ n++; END print 0+n'
            – Stéphane Chazelas
            Jun 28 '17 at 20:23




            @Techiee, or just awk '/p1/ && /p2/ n++; END print 0+n'
            – Stéphane Chazelas
            Jun 28 '17 at 20:23












            up vote
            16
            down vote













            You didn't specify grep version, this is important. Some regexp engines allow multiple matching groupped by AND using '&' but this is non-standard and non-portable feature. But, at least GNU grep doesn't support this.



            OTOH you can simply replace grep with sed, awk, perl, etc. (listed in order of weight increasing). With awk, the command would look like




            awk '/regexp1/ && /regexp2/ && /regexp3/ print; '


            and it can be constructed to be specified in command line in easy way.






            share|improve this answer

















            • 3




              Just remember that awk uses ERE's, e.g. the equivalent of grep -E, as opposed to the BRE's that plain grep uses.
              – jw013
              Nov 10 '12 at 9:42






            • 3




              awk's regexes are called EREs, but in fact they're a bit idiosyncratic. Here are probably more details than anyone cares for: wiki.alpinelinux.org/wiki/Regex
              – dubiousjim
              Nov 10 '12 at 15:35











            • Thank you, grep 2.7.3 (openSUSE). I upvoted you, but I will keep question open for a while, maybe there is some trick for grep (not that I dislike awk -- simply knowing more is better).
              – greenoldman
              Nov 10 '12 at 15:42







            • 1




              The default action is to print the matching line so the print; part isn't really necessary or useful here.
              – tripleee
              Apr 20 '17 at 11:58















            up vote
            16
            down vote













            You didn't specify grep version, this is important. Some regexp engines allow multiple matching groupped by AND using '&' but this is non-standard and non-portable feature. But, at least GNU grep doesn't support this.



            OTOH you can simply replace grep with sed, awk, perl, etc. (listed in order of weight increasing). With awk, the command would look like




            awk '/regexp1/ && /regexp2/ && /regexp3/ print; '


            and it can be constructed to be specified in command line in easy way.






            share|improve this answer

















            • 3




              Just remember that awk uses ERE's, e.g. the equivalent of grep -E, as opposed to the BRE's that plain grep uses.
              – jw013
              Nov 10 '12 at 9:42






            • 3




              awk's regexes are called EREs, but in fact they're a bit idiosyncratic. Here are probably more details than anyone cares for: wiki.alpinelinux.org/wiki/Regex
              – dubiousjim
              Nov 10 '12 at 15:35











            • Thank you, grep 2.7.3 (openSUSE). I upvoted you, but I will keep question open for a while, maybe there is some trick for grep (not that I dislike awk -- simply knowing more is better).
              – greenoldman
              Nov 10 '12 at 15:42







            • 1




              The default action is to print the matching line so the print; part isn't really necessary or useful here.
              – tripleee
              Apr 20 '17 at 11:58













            up vote
            16
            down vote










            up vote
            16
            down vote









            You didn't specify grep version, this is important. Some regexp engines allow multiple matching groupped by AND using '&' but this is non-standard and non-portable feature. But, at least GNU grep doesn't support this.



            OTOH you can simply replace grep with sed, awk, perl, etc. (listed in order of weight increasing). With awk, the command would look like




            awk '/regexp1/ && /regexp2/ && /regexp3/ print; '


            and it can be constructed to be specified in command line in easy way.






            share|improve this answer













            You didn't specify grep version, this is important. Some regexp engines allow multiple matching groupped by AND using '&' but this is non-standard and non-portable feature. But, at least GNU grep doesn't support this.



            OTOH you can simply replace grep with sed, awk, perl, etc. (listed in order of weight increasing). With awk, the command would look like




            awk '/regexp1/ && /regexp2/ && /regexp3/ print; '


            and it can be constructed to be specified in command line in easy way.







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Nov 10 '12 at 8:34









            Netch

            1,764810




            1,764810







            • 3




              Just remember that awk uses ERE's, e.g. the equivalent of grep -E, as opposed to the BRE's that plain grep uses.
              – jw013
              Nov 10 '12 at 9:42






            • 3




              awk's regexes are called EREs, but in fact they're a bit idiosyncratic. Here are probably more details than anyone cares for: wiki.alpinelinux.org/wiki/Regex
              – dubiousjim
              Nov 10 '12 at 15:35











            • Thank you, grep 2.7.3 (openSUSE). I upvoted you, but I will keep question open for a while, maybe there is some trick for grep (not that I dislike awk -- simply knowing more is better).
              – greenoldman
              Nov 10 '12 at 15:42







            • 1




              The default action is to print the matching line so the print; part isn't really necessary or useful here.
              – tripleee
              Apr 20 '17 at 11:58













            • 3




              Just remember that awk uses ERE's, e.g. the equivalent of grep -E, as opposed to the BRE's that plain grep uses.
              – jw013
              Nov 10 '12 at 9:42






            • 3




              awk's regexes are called EREs, but in fact they're a bit idiosyncratic. Here are probably more details than anyone cares for: wiki.alpinelinux.org/wiki/Regex
              – dubiousjim
              Nov 10 '12 at 15:35











            • Thank you, grep 2.7.3 (openSUSE). I upvoted you, but I will keep question open for a while, maybe there is some trick for grep (not that I dislike awk -- simply knowing more is better).
              – greenoldman
              Nov 10 '12 at 15:42







            • 1




              The default action is to print the matching line so the print; part isn't really necessary or useful here.
              – tripleee
              Apr 20 '17 at 11:58








            3




            3




            Just remember that awk uses ERE's, e.g. the equivalent of grep -E, as opposed to the BRE's that plain grep uses.
            – jw013
            Nov 10 '12 at 9:42




            Just remember that awk uses ERE's, e.g. the equivalent of grep -E, as opposed to the BRE's that plain grep uses.
            – jw013
            Nov 10 '12 at 9:42




            3




            3




            awk's regexes are called EREs, but in fact they're a bit idiosyncratic. Here are probably more details than anyone cares for: wiki.alpinelinux.org/wiki/Regex
            – dubiousjim
            Nov 10 '12 at 15:35





            awk's regexes are called EREs, but in fact they're a bit idiosyncratic. Here are probably more details than anyone cares for: wiki.alpinelinux.org/wiki/Regex
            – dubiousjim
            Nov 10 '12 at 15:35













            Thank you, grep 2.7.3 (openSUSE). I upvoted you, but I will keep question open for a while, maybe there is some trick for grep (not that I dislike awk -- simply knowing more is better).
            – greenoldman
            Nov 10 '12 at 15:42





            Thank you, grep 2.7.3 (openSUSE). I upvoted you, but I will keep question open for a while, maybe there is some trick for grep (not that I dislike awk -- simply knowing more is better).
            – greenoldman
            Nov 10 '12 at 15:42





            1




            1




            The default action is to print the matching line so the print; part isn't really necessary or useful here.
            – tripleee
            Apr 20 '17 at 11:58





            The default action is to print the matching line so the print; part isn't really necessary or useful here.
            – tripleee
            Apr 20 '17 at 11:58











            up vote
            6
            down vote













            This is not a very good solution but illustrates a somewhat cool "trick"



            function chained-grep() chained-grep "$@"


            cat something | chained-grep all patterns must match order but matter dont





            share|improve this answer

























              up vote
              6
              down vote













              This is not a very good solution but illustrates a somewhat cool "trick"



              function chained-grep() chained-grep "$@"


              cat something | chained-grep all patterns must match order but matter dont





              share|improve this answer























                up vote
                6
                down vote










                up vote
                6
                down vote









                This is not a very good solution but illustrates a somewhat cool "trick"



                function chained-grep() chained-grep "$@"


                cat something | chained-grep all patterns must match order but matter dont





                share|improve this answer













                This is not a very good solution but illustrates a somewhat cool "trick"



                function chained-grep() chained-grep "$@"


                cat something | chained-grep all patterns must match order but matter dont






                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Nov 26 '16 at 22:13









                olejorgenb

                43637




                43637




















                    up vote
                    6
                    down vote



                    +50












                    If patterns.txt contains one pattern per line, you can do something like this:



                    awk 'NR==FNRa[$0];nextfor(i in a)if($0!~i)next1' patterns.txt -


                    To search for fixed strings instead of regular expressions, use this variant:



                    awk 'NR==FNRa[$0];nextfor(i in a)if(!index($0,i))next1' patterns.txt -


                    To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.






                    share|improve this answer



























                      up vote
                      6
                      down vote



                      +50












                      If patterns.txt contains one pattern per line, you can do something like this:



                      awk 'NR==FNRa[$0];nextfor(i in a)if($0!~i)next1' patterns.txt -


                      To search for fixed strings instead of regular expressions, use this variant:



                      awk 'NR==FNRa[$0];nextfor(i in a)if(!index($0,i))next1' patterns.txt -


                      To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.






                      share|improve this answer

























                        up vote
                        6
                        down vote



                        +50







                        up vote
                        6
                        down vote



                        +50




                        +50






                        If patterns.txt contains one pattern per line, you can do something like this:



                        awk 'NR==FNRa[$0];nextfor(i in a)if($0!~i)next1' patterns.txt -


                        To search for fixed strings instead of regular expressions, use this variant:



                        awk 'NR==FNRa[$0];nextfor(i in a)if(!index($0,i))next1' patterns.txt -


                        To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.






                        share|improve this answer

















                        If patterns.txt contains one pattern per line, you can do something like this:



                        awk 'NR==FNRa[$0];nextfor(i in a)if($0!~i)next1' patterns.txt -


                        To search for fixed strings instead of regular expressions, use this variant:



                        awk 'NR==FNRa[$0];nextfor(i in a)if(!index($0,i))next1' patterns.txt -


                        To print all instead of no lines of the input in the case that patterns.txt is empty, replace NR==FNR with FILENAME==ARGV[1] or with ARGIND==1 in gawk.







                        share|improve this answer















                        share|improve this answer



                        share|improve this answer








                        edited Dec 25 '16 at 10:47









                        Alex Stragies

                        3,0891436




                        3,0891436











                        answered Mar 18 '16 at 15:25









                        user4669748

                        50946




                        50946




















                            up vote
                            2
                            down vote













                            git grep



                            Here is the syntax using git grep combining multiple patterns using Boolean expressions:



                            git grep -e pattern1 --and -e pattern2 --and -e pattern3


                            The above command will print lines matching all the patterns at once.



                            If the files aren't under version control, add --no-index param.




                            Search files in the current directory that is not managed by Git.




                            Check man git-grep for help.



                            See also: Check if all of multiple strings or regexes exist in a file.






                            share|improve this answer



























                              up vote
                              2
                              down vote













                              git grep



                              Here is the syntax using git grep combining multiple patterns using Boolean expressions:



                              git grep -e pattern1 --and -e pattern2 --and -e pattern3


                              The above command will print lines matching all the patterns at once.



                              If the files aren't under version control, add --no-index param.




                              Search files in the current directory that is not managed by Git.




                              Check man git-grep for help.



                              See also: Check if all of multiple strings or regexes exist in a file.






                              share|improve this answer

























                                up vote
                                2
                                down vote










                                up vote
                                2
                                down vote









                                git grep



                                Here is the syntax using git grep combining multiple patterns using Boolean expressions:



                                git grep -e pattern1 --and -e pattern2 --and -e pattern3


                                The above command will print lines matching all the patterns at once.



                                If the files aren't under version control, add --no-index param.




                                Search files in the current directory that is not managed by Git.




                                Check man git-grep for help.



                                See also: Check if all of multiple strings or regexes exist in a file.






                                share|improve this answer















                                git grep



                                Here is the syntax using git grep combining multiple patterns using Boolean expressions:



                                git grep -e pattern1 --and -e pattern2 --and -e pattern3


                                The above command will print lines matching all the patterns at once.



                                If the files aren't under version control, add --no-index param.




                                Search files in the current directory that is not managed by Git.




                                Check man git-grep for help.



                                See also: Check if all of multiple strings or regexes exist in a file.







                                share|improve this answer















                                share|improve this answer



                                share|improve this answer








                                edited Apr 15 at 1:57


























                                answered Apr 11 at 0:53









                                kenorb

                                7,32836296




                                7,32836296




















                                    up vote
                                    2
                                    down vote













                                    This shell command may help:



                                    eval "</dev/stdin $(printf "|grep '%s'" pattern1 pattern2)" FILE


                                    However it's easier if all your patterns are stored in the file, so you can define the following alias:



                                    alias grep-all="cat $(xargs printf '| grep "%s"' < patterns.txt)"


                                    and use it as:



                                    cat text.txt | grep-all


                                    You can of course modify the alias depending on your required syntax, so with this alias:



                                    alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < patterns.txt)"


                                    you can use just a single command, e.g.



                                    grep-all text.txt


                                    For more ideas, check also: Match all patterns from file at once.



                                    For AND operation per file, see: Check if all of multiple strings or regexes exist in a file.






                                    share|improve this answer



























                                      up vote
                                      2
                                      down vote













                                      This shell command may help:



                                      eval "</dev/stdin $(printf "|grep '%s'" pattern1 pattern2)" FILE


                                      However it's easier if all your patterns are stored in the file, so you can define the following alias:



                                      alias grep-all="cat $(xargs printf '| grep "%s"' < patterns.txt)"


                                      and use it as:



                                      cat text.txt | grep-all


                                      You can of course modify the alias depending on your required syntax, so with this alias:



                                      alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < patterns.txt)"


                                      you can use just a single command, e.g.



                                      grep-all text.txt


                                      For more ideas, check also: Match all patterns from file at once.



                                      For AND operation per file, see: Check if all of multiple strings or regexes exist in a file.






                                      share|improve this answer

























                                        up vote
                                        2
                                        down vote










                                        up vote
                                        2
                                        down vote









                                        This shell command may help:



                                        eval "</dev/stdin $(printf "|grep '%s'" pattern1 pattern2)" FILE


                                        However it's easier if all your patterns are stored in the file, so you can define the following alias:



                                        alias grep-all="cat $(xargs printf '| grep "%s"' < patterns.txt)"


                                        and use it as:



                                        cat text.txt | grep-all


                                        You can of course modify the alias depending on your required syntax, so with this alias:



                                        alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < patterns.txt)"


                                        you can use just a single command, e.g.



                                        grep-all text.txt


                                        For more ideas, check also: Match all patterns from file at once.



                                        For AND operation per file, see: Check if all of multiple strings or regexes exist in a file.






                                        share|improve this answer















                                        This shell command may help:



                                        eval "</dev/stdin $(printf "|grep '%s'" pattern1 pattern2)" FILE


                                        However it's easier if all your patterns are stored in the file, so you can define the following alias:



                                        alias grep-all="cat $(xargs printf '| grep "%s"' < patterns.txt)"


                                        and use it as:



                                        cat text.txt | grep-all


                                        You can of course modify the alias depending on your required syntax, so with this alias:



                                        alias grep-all="</dev/stdin $(xargs printf '|grep "%s"' < patterns.txt)"


                                        you can use just a single command, e.g.



                                        grep-all text.txt


                                        For more ideas, check also: Match all patterns from file at once.



                                        For AND operation per file, see: Check if all of multiple strings or regexes exist in a file.







                                        share|improve this answer















                                        share|improve this answer



                                        share|improve this answer








                                        edited Apr 19 at 18:10


























                                        answered Dec 22 '16 at 16:01









                                        kenorb

                                        7,32836296




                                        7,32836296




















                                            up vote
                                            0
                                            down vote













                                            ripgrep



                                            Here is the example using rg:



                                            rg -N '(?P<p1>.*pattern1.*)(?P<p2>.*pattern2.*)(?P<p3>.*pattern3.*)' file.txt


                                            It's one of the quickest grepping tools, since it's built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.



                                            See also related feature request at GH-875.






                                            share|improve this answer



























                                              up vote
                                              0
                                              down vote













                                              ripgrep



                                              Here is the example using rg:



                                              rg -N '(?P<p1>.*pattern1.*)(?P<p2>.*pattern2.*)(?P<p3>.*pattern3.*)' file.txt


                                              It's one of the quickest grepping tools, since it's built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.



                                              See also related feature request at GH-875.






                                              share|improve this answer

























                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                ripgrep



                                                Here is the example using rg:



                                                rg -N '(?P<p1>.*pattern1.*)(?P<p2>.*pattern2.*)(?P<p3>.*pattern3.*)' file.txt


                                                It's one of the quickest grepping tools, since it's built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.



                                                See also related feature request at GH-875.






                                                share|improve this answer















                                                ripgrep



                                                Here is the example using rg:



                                                rg -N '(?P<p1>.*pattern1.*)(?P<p2>.*pattern2.*)(?P<p3>.*pattern3.*)' file.txt


                                                It's one of the quickest grepping tools, since it's built on top of Rust's regex engine which uses finite automata, SIMD and aggressive literal optimizations to make searching very fast.



                                                See also related feature request at GH-875.







                                                share|improve this answer















                                                share|improve this answer



                                                share|improve this answer








                                                edited Apr 11 at 9:18


























                                                answered Apr 11 at 1:19









                                                kenorb

                                                7,32836296




                                                7,32836296






















                                                     

                                                    draft saved


                                                    draft discarded


























                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f55359%2fhow-to-run-grep-with-multiple-and-patterns%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest













































































                                                    Popular posts from this blog

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

                                                    Displaying single band from multi-band raster using QGIS

                                                    How many registers does an x86_64 CPU actually have?