grep with logic operators

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











up vote
13
down vote

favorite
7












Is there a grep-like utility that will enable me to do grep searches with logic operators. I want to be able to nest and combine the logical constructs freely. For example, stuff like this should be possible:



grep (term1 && term2) || (term1 && (term3 xor term4)) *


I realize this can be done with vanilla grep and additional bash scripting, but my goal here is to avoid having to do that.










share|improve this question



























    up vote
    13
    down vote

    favorite
    7












    Is there a grep-like utility that will enable me to do grep searches with logic operators. I want to be able to nest and combine the logical constructs freely. For example, stuff like this should be possible:



    grep (term1 && term2) || (term1 && (term3 xor term4)) *


    I realize this can be done with vanilla grep and additional bash scripting, but my goal here is to avoid having to do that.










    share|improve this question

























      up vote
      13
      down vote

      favorite
      7









      up vote
      13
      down vote

      favorite
      7






      7





      Is there a grep-like utility that will enable me to do grep searches with logic operators. I want to be able to nest and combine the logical constructs freely. For example, stuff like this should be possible:



      grep (term1 && term2) || (term1 && (term3 xor term4)) *


      I realize this can be done with vanilla grep and additional bash scripting, but my goal here is to avoid having to do that.










      share|improve this question















      Is there a grep-like utility that will enable me to do grep searches with logic operators. I want to be able to nest and combine the logical constructs freely. For example, stuff like this should be possible:



      grep (term1 && term2) || (term1 && (term3 xor term4)) *


      I realize this can be done with vanilla grep and additional bash scripting, but my goal here is to avoid having to do that.







      grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 5 '15 at 11:53

























      asked Jan 5 '15 at 11:21









      Nikita Kiryanov

      75116




      75116




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          10
          down vote



          accepted










          With awk, as with perl, you'll have to wrap terms in //, but it can be done:



          awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 





          share|improve this answer



























            up vote
            21
            down vote













            There are lot of ways to use grep with logical operators.




            1. Use
              | to separate multiple patterns for the OR condition.



              Example: grep 'pattern1|pattern2' filename




            2. Use the -E option to send multiple patterns for the OR
              condition.



              Example: grep -E 'pattern1|pattern2' filename




            3. Using a single -e matches only one pattern, but using multiple
              -e option matches more than one pattern.



              Example: grep -e pattern1 -e pattern2 filename



            4. grep -v can simulate the NOT operation.



            5. There is no AND operator in grep, but you can brute-force
              simulate AND by using the -E option.



              Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename



              The above example will match all the lines that contain both
              pattern1 and pattern2 in either order.)







            share|improve this answer


















            • 1




              -E is not quite equivalent to && due to the fact that it is order-sensitive
              – iruvar
              Jan 5 '15 at 13:58










            • grep foo | grep bar is a more general way to do AND.
              – Kenster
              Jan 5 '15 at 15:43






            • 1




              +1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
              – Eric Hepperle - CodeSlayer2010
              Apr 20 '17 at 15:57

















            up vote
            5
            down vote













            You could use perl:



            perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'





            share|improve this answer





























              up vote
              0
              down vote













              sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *


              I believe that accomplishes what you're trying to do. It deletes from output any line which doesn't match term1, it branches out of the script (and so autoprints) any line that remains and that matches term2, and for lines that remain it deletes any which do not match term3 and from those any that do match term4.



              sed scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.






              share|improve this answer



























                up vote
                0
                down vote













                I use to chain grep commands to achieve a logical AND:



                grep expr1 filename | grep expr2


                I believe it is pretty straightforward, Unix-like and elegant.




                Then you can combine (as @Tushi thoroughly explained) with the -E option for OR-ing and -v for negating.



                Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).






                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%2f177513%2fgrep-with-logic-operators%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  10
                  down vote



                  accepted










                  With awk, as with perl, you'll have to wrap terms in //, but it can be done:



                  awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 





                  share|improve this answer
























                    up vote
                    10
                    down vote



                    accepted










                    With awk, as with perl, you'll have to wrap terms in //, but it can be done:



                    awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 





                    share|improve this answer






















                      up vote
                      10
                      down vote



                      accepted







                      up vote
                      10
                      down vote



                      accepted






                      With awk, as with perl, you'll have to wrap terms in //, but it can be done:



                      awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 





                      share|improve this answer












                      With awk, as with perl, you'll have to wrap terms in //, but it can be done:



                      awk '(/term1/ && /term2/) || (/term1/ && xor(/term3/, /term4/))' 






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jan 5 '15 at 12:31









                      muru

                      33.9k578147




                      33.9k578147






















                          up vote
                          21
                          down vote













                          There are lot of ways to use grep with logical operators.




                          1. Use
                            | to separate multiple patterns for the OR condition.



                            Example: grep 'pattern1|pattern2' filename




                          2. Use the -E option to send multiple patterns for the OR
                            condition.



                            Example: grep -E 'pattern1|pattern2' filename




                          3. Using a single -e matches only one pattern, but using multiple
                            -e option matches more than one pattern.



                            Example: grep -e pattern1 -e pattern2 filename



                          4. grep -v can simulate the NOT operation.



                          5. There is no AND operator in grep, but you can brute-force
                            simulate AND by using the -E option.



                            Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename



                            The above example will match all the lines that contain both
                            pattern1 and pattern2 in either order.)







                          share|improve this answer


















                          • 1




                            -E is not quite equivalent to && due to the fact that it is order-sensitive
                            – iruvar
                            Jan 5 '15 at 13:58










                          • grep foo | grep bar is a more general way to do AND.
                            – Kenster
                            Jan 5 '15 at 15:43






                          • 1




                            +1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
                            – Eric Hepperle - CodeSlayer2010
                            Apr 20 '17 at 15:57














                          up vote
                          21
                          down vote













                          There are lot of ways to use grep with logical operators.




                          1. Use
                            | to separate multiple patterns for the OR condition.



                            Example: grep 'pattern1|pattern2' filename




                          2. Use the -E option to send multiple patterns for the OR
                            condition.



                            Example: grep -E 'pattern1|pattern2' filename




                          3. Using a single -e matches only one pattern, but using multiple
                            -e option matches more than one pattern.



                            Example: grep -e pattern1 -e pattern2 filename



                          4. grep -v can simulate the NOT operation.



                          5. There is no AND operator in grep, but you can brute-force
                            simulate AND by using the -E option.



                            Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename



                            The above example will match all the lines that contain both
                            pattern1 and pattern2 in either order.)







                          share|improve this answer


















                          • 1




                            -E is not quite equivalent to && due to the fact that it is order-sensitive
                            – iruvar
                            Jan 5 '15 at 13:58










                          • grep foo | grep bar is a more general way to do AND.
                            – Kenster
                            Jan 5 '15 at 15:43






                          • 1




                            +1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
                            – Eric Hepperle - CodeSlayer2010
                            Apr 20 '17 at 15:57












                          up vote
                          21
                          down vote










                          up vote
                          21
                          down vote









                          There are lot of ways to use grep with logical operators.




                          1. Use
                            | to separate multiple patterns for the OR condition.



                            Example: grep 'pattern1|pattern2' filename




                          2. Use the -E option to send multiple patterns for the OR
                            condition.



                            Example: grep -E 'pattern1|pattern2' filename




                          3. Using a single -e matches only one pattern, but using multiple
                            -e option matches more than one pattern.



                            Example: grep -e pattern1 -e pattern2 filename



                          4. grep -v can simulate the NOT operation.



                          5. There is no AND operator in grep, but you can brute-force
                            simulate AND by using the -E option.



                            Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename



                            The above example will match all the lines that contain both
                            pattern1 and pattern2 in either order.)







                          share|improve this answer














                          There are lot of ways to use grep with logical operators.




                          1. Use
                            | to separate multiple patterns for the OR condition.



                            Example: grep 'pattern1|pattern2' filename




                          2. Use the -E option to send multiple patterns for the OR
                            condition.



                            Example: grep -E 'pattern1|pattern2' filename




                          3. Using a single -e matches only one pattern, but using multiple
                            -e option matches more than one pattern.



                            Example: grep -e pattern1 -e pattern2 filename



                          4. grep -v can simulate the NOT operation.



                          5. There is no AND operator in grep, but you can brute-force
                            simulate AND by using the -E option.



                            Example : grep -E 'pattern1.*pattern2|pattern2.*pattern1' filename



                            The above example will match all the lines that contain both
                            pattern1 and pattern2 in either order.)








                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 15 '17 at 13:54









                          agc

                          4,3221935




                          4,3221935










                          answered Jan 5 '15 at 12:12









                          Thushi

                          6,06621137




                          6,06621137







                          • 1




                            -E is not quite equivalent to && due to the fact that it is order-sensitive
                            – iruvar
                            Jan 5 '15 at 13:58










                          • grep foo | grep bar is a more general way to do AND.
                            – Kenster
                            Jan 5 '15 at 15:43






                          • 1




                            +1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
                            – Eric Hepperle - CodeSlayer2010
                            Apr 20 '17 at 15:57












                          • 1




                            -E is not quite equivalent to && due to the fact that it is order-sensitive
                            – iruvar
                            Jan 5 '15 at 13:58










                          • grep foo | grep bar is a more general way to do AND.
                            – Kenster
                            Jan 5 '15 at 15:43






                          • 1




                            +1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
                            – Eric Hepperle - CodeSlayer2010
                            Apr 20 '17 at 15:57







                          1




                          1




                          -E is not quite equivalent to && due to the fact that it is order-sensitive
                          – iruvar
                          Jan 5 '15 at 13:58




                          -E is not quite equivalent to && due to the fact that it is order-sensitive
                          – iruvar
                          Jan 5 '15 at 13:58












                          grep foo | grep bar is a more general way to do AND.
                          – Kenster
                          Jan 5 '15 at 15:43




                          grep foo | grep bar is a more general way to do AND.
                          – Kenster
                          Jan 5 '15 at 15:43




                          1




                          1




                          +1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
                          – Eric Hepperle - CodeSlayer2010
                          Apr 20 '17 at 15:57




                          +1 for clearly explaining that there isn't a real "AND" operator, and that the best we can do is simulate a hack using an OR structure. This will of course get unwieldy with more than 3 terms, but for two terms it works well.
                          – Eric Hepperle - CodeSlayer2010
                          Apr 20 '17 at 15:57










                          up vote
                          5
                          down vote













                          You could use perl:



                          perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'





                          share|improve this answer


























                            up vote
                            5
                            down vote













                            You could use perl:



                            perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'





                            share|improve this answer
























                              up vote
                              5
                              down vote










                              up vote
                              5
                              down vote









                              You could use perl:



                              perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'





                              share|improve this answer














                              You could use perl:



                              perl -wne 'print if (/term1/ && /term2/) || (/term1/ && (/term3/ xor /term4/))'






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 5 '15 at 13:26

























                              answered Jan 5 '15 at 11:49









                              michas

                              14.6k33569




                              14.6k33569




















                                  up vote
                                  0
                                  down vote













                                  sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *


                                  I believe that accomplishes what you're trying to do. It deletes from output any line which doesn't match term1, it branches out of the script (and so autoprints) any line that remains and that matches term2, and for lines that remain it deletes any which do not match term3 and from those any that do match term4.



                                  sed scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.






                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *


                                    I believe that accomplishes what you're trying to do. It deletes from output any line which doesn't match term1, it branches out of the script (and so autoprints) any line that remains and that matches term2, and for lines that remain it deletes any which do not match term3 and from those any that do match term4.



                                    sed scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.






                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *


                                      I believe that accomplishes what you're trying to do. It deletes from output any line which doesn't match term1, it branches out of the script (and so autoprints) any line that remains and that matches term2, and for lines that remain it deletes any which do not match term3 and from those any that do match term4.



                                      sed scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.






                                      share|improve this answer












                                      sed '/term1/!d;/term2/b' -e '/term3/!d;/term4/d' *


                                      I believe that accomplishes what you're trying to do. It deletes from output any line which doesn't match term1, it branches out of the script (and so autoprints) any line that remains and that matches term2, and for lines that remain it deletes any which do not match term3 and from those any that do match term4.



                                      sed scripts are evaluated in order, and all tests are boolean, so any actions resulting from a test are going to directly affect the behavior of any following actions.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jan 5 '15 at 17:24









                                      mikeserv

                                      44.6k565150




                                      44.6k565150




















                                          up vote
                                          0
                                          down vote













                                          I use to chain grep commands to achieve a logical AND:



                                          grep expr1 filename | grep expr2


                                          I believe it is pretty straightforward, Unix-like and elegant.




                                          Then you can combine (as @Tushi thoroughly explained) with the -E option for OR-ing and -v for negating.



                                          Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).






                                          share|improve this answer
























                                            up vote
                                            0
                                            down vote













                                            I use to chain grep commands to achieve a logical AND:



                                            grep expr1 filename | grep expr2


                                            I believe it is pretty straightforward, Unix-like and elegant.




                                            Then you can combine (as @Tushi thoroughly explained) with the -E option for OR-ing and -v for negating.



                                            Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).






                                            share|improve this answer






















                                              up vote
                                              0
                                              down vote










                                              up vote
                                              0
                                              down vote









                                              I use to chain grep commands to achieve a logical AND:



                                              grep expr1 filename | grep expr2


                                              I believe it is pretty straightforward, Unix-like and elegant.




                                              Then you can combine (as @Tushi thoroughly explained) with the -E option for OR-ing and -v for negating.



                                              Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).






                                              share|improve this answer












                                              I use to chain grep commands to achieve a logical AND:



                                              grep expr1 filename | grep expr2


                                              I believe it is pretty straightforward, Unix-like and elegant.




                                              Then you can combine (as @Tushi thoroughly explained) with the -E option for OR-ing and -v for negating.



                                              Your specific example is pretty nasty and probably would benefit from some more powerful utility (see @muru's answer).







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Oct 4 at 8:02









                                              Campa

                                              1033




                                              1033



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f177513%2fgrep-with-logic-operators%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?