validate result of grep

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











up vote
1
down vote

favorite












I'm trying with the below code to print Available if there's a match, else nil



grep -o 'pattern' test.log | awk 'if($0=="pattern") print "Available"; else print "nil"'


The if part is working fine, but I'm not getting the else part if the grepped out is null.










share|improve this question



























    up vote
    1
    down vote

    favorite












    I'm trying with the below code to print Available if there's a match, else nil



    grep -o 'pattern' test.log | awk 'if($0=="pattern") print "Available"; else print "nil"'


    The if part is working fine, but I'm not getting the else part if the grepped out is null.










    share|improve this question

























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm trying with the below code to print Available if there's a match, else nil



      grep -o 'pattern' test.log | awk 'if($0=="pattern") print "Available"; else print "nil"'


      The if part is working fine, but I'm not getting the else part if the grepped out is null.










      share|improve this question















      I'm trying with the below code to print Available if there's a match, else nil



      grep -o 'pattern' test.log | awk 'if($0=="pattern") print "Available"; else print "nil"'


      The if part is working fine, but I'm not getting the else part if the grepped out is null.







      awk grep






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 12 at 13:04









      Jeff Schaller

      32.4k849110




      32.4k849110










      asked Aug 7 at 8:21







      user304107



























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          If the pattern did not match, then grep produces no output and the awk program has no data to work with. This is why you will never get nil from the awk code.



          Another way of doing this would be



          if grep -q 'pattern' test.log; then
          echo 'Available'
          else
          echo 'nil'
          fi


          The -q option of grep is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep, only its exit status.




          With awk, you could still do your test if you wish, but you will have to output the nil string conditionally in an END block:



          grep -o 'pattern' test.log |
          awk '/pattern/ print "Available"; found = 1
          END if (!found) print "nil" '


          The END block will be executed even if the awk script does not get any input to work with.



          In fact, you could do the whole thing with awk:



          awk '/pattern/ print "Available"; found = 1; exit 
          END if (!found) print "nil" ' test.log


          Calling exit will invoke the END block, so we can't get rid of the found variable.






          share|improve this answer






















          • i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
            – user304107
            Aug 7 at 9:12











          • @reddingr Why would you want a one-liner? You could use grep -q 'pattern' test.log && echo 'Available' || echo 'nil' if you feel so inclined.
            – Kusalananda
            Aug 7 at 9:15










          • Great!! not aware of option -q ... Thanks.
            – user304107
            Aug 7 at 9:21










          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%2f460997%2fvalidate-result-of-grep%23new-answer', 'question_page');

          );

          Post as a guest





























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          If the pattern did not match, then grep produces no output and the awk program has no data to work with. This is why you will never get nil from the awk code.



          Another way of doing this would be



          if grep -q 'pattern' test.log; then
          echo 'Available'
          else
          echo 'nil'
          fi


          The -q option of grep is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep, only its exit status.




          With awk, you could still do your test if you wish, but you will have to output the nil string conditionally in an END block:



          grep -o 'pattern' test.log |
          awk '/pattern/ print "Available"; found = 1
          END if (!found) print "nil" '


          The END block will be executed even if the awk script does not get any input to work with.



          In fact, you could do the whole thing with awk:



          awk '/pattern/ print "Available"; found = 1; exit 
          END if (!found) print "nil" ' test.log


          Calling exit will invoke the END block, so we can't get rid of the found variable.






          share|improve this answer






















          • i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
            – user304107
            Aug 7 at 9:12











          • @reddingr Why would you want a one-liner? You could use grep -q 'pattern' test.log && echo 'Available' || echo 'nil' if you feel so inclined.
            – Kusalananda
            Aug 7 at 9:15










          • Great!! not aware of option -q ... Thanks.
            – user304107
            Aug 7 at 9:21














          up vote
          2
          down vote



          accepted










          If the pattern did not match, then grep produces no output and the awk program has no data to work with. This is why you will never get nil from the awk code.



          Another way of doing this would be



          if grep -q 'pattern' test.log; then
          echo 'Available'
          else
          echo 'nil'
          fi


          The -q option of grep is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep, only its exit status.




          With awk, you could still do your test if you wish, but you will have to output the nil string conditionally in an END block:



          grep -o 'pattern' test.log |
          awk '/pattern/ print "Available"; found = 1
          END if (!found) print "nil" '


          The END block will be executed even if the awk script does not get any input to work with.



          In fact, you could do the whole thing with awk:



          awk '/pattern/ print "Available"; found = 1; exit 
          END if (!found) print "nil" ' test.log


          Calling exit will invoke the END block, so we can't get rid of the found variable.






          share|improve this answer






















          • i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
            – user304107
            Aug 7 at 9:12











          • @reddingr Why would you want a one-liner? You could use grep -q 'pattern' test.log && echo 'Available' || echo 'nil' if you feel so inclined.
            – Kusalananda
            Aug 7 at 9:15










          • Great!! not aware of option -q ... Thanks.
            – user304107
            Aug 7 at 9:21












          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          If the pattern did not match, then grep produces no output and the awk program has no data to work with. This is why you will never get nil from the awk code.



          Another way of doing this would be



          if grep -q 'pattern' test.log; then
          echo 'Available'
          else
          echo 'nil'
          fi


          The -q option of grep is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep, only its exit status.




          With awk, you could still do your test if you wish, but you will have to output the nil string conditionally in an END block:



          grep -o 'pattern' test.log |
          awk '/pattern/ print "Available"; found = 1
          END if (!found) print "nil" '


          The END block will be executed even if the awk script does not get any input to work with.



          In fact, you could do the whole thing with awk:



          awk '/pattern/ print "Available"; found = 1; exit 
          END if (!found) print "nil" ' test.log


          Calling exit will invoke the END block, so we can't get rid of the found variable.






          share|improve this answer














          If the pattern did not match, then grep produces no output and the awk program has no data to work with. This is why you will never get nil from the awk code.



          Another way of doing this would be



          if grep -q 'pattern' test.log; then
          echo 'Available'
          else
          echo 'nil'
          fi


          The -q option of grep is used to stop the utility from generating any output (apart from possibly diagnostic output). Here, we don't want output from grep, only its exit status.




          With awk, you could still do your test if you wish, but you will have to output the nil string conditionally in an END block:



          grep -o 'pattern' test.log |
          awk '/pattern/ print "Available"; found = 1
          END if (!found) print "nil" '


          The END block will be executed even if the awk script does not get any input to work with.



          In fact, you could do the whole thing with awk:



          awk '/pattern/ print "Available"; found = 1; exit 
          END if (!found) print "nil" ' test.log


          Calling exit will invoke the END block, so we can't get rid of the found variable.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Aug 7 at 9:10

























          answered Aug 7 at 8:25









          Kusalananda

          106k14209327




          106k14209327











          • i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
            – user304107
            Aug 7 at 9:12











          • @reddingr Why would you want a one-liner? You could use grep -q 'pattern' test.log && echo 'Available' || echo 'nil' if you feel so inclined.
            – Kusalananda
            Aug 7 at 9:15










          • Great!! not aware of option -q ... Thanks.
            – user304107
            Aug 7 at 9:21
















          • i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
            – user304107
            Aug 7 at 9:12











          • @reddingr Why would you want a one-liner? You could use grep -q 'pattern' test.log && echo 'Available' || echo 'nil' if you feel so inclined.
            – Kusalananda
            Aug 7 at 9:15










          • Great!! not aware of option -q ... Thanks.
            – user304107
            Aug 7 at 9:21















          i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
          – user304107
          Aug 7 at 9:12





          i was doing with your first option. But i am looking for one-liner, the second option with awk works great ... Thanks.
          – user304107
          Aug 7 at 9:12













          @reddingr Why would you want a one-liner? You could use grep -q 'pattern' test.log && echo 'Available' || echo 'nil' if you feel so inclined.
          – Kusalananda
          Aug 7 at 9:15




          @reddingr Why would you want a one-liner? You could use grep -q 'pattern' test.log && echo 'Available' || echo 'nil' if you feel so inclined.
          – Kusalananda
          Aug 7 at 9:15












          Great!! not aware of option -q ... Thanks.
          – user304107
          Aug 7 at 9:21




          Great!! not aware of option -q ... Thanks.
          – user304107
          Aug 7 at 9:21

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f460997%2fvalidate-result-of-grep%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?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay