awk to search the string and count for total in a one liner [duplicate]












4















This question already has an answer here:




  • Count records matching pattern with Awk

    3 answers




I have logs like as given sample below, where I'm simply looking for a SUCCESS string in the log file and counting the total.



$ cat ansible.log
lnx-host01.tin.com | SUCCESS => {"changed": false, "ping": "pong"}
lnx-host02.tin.com | SUCCESS => {"changed": false, "ping": "pong"}


This is what the simple straightforward way, but I'm wondering if it can be done with awk itself as a one liner without passing to the wc command.



$ awk '/SUCCESS/{print $0}'  ansible.log | wc -l
66

OR

$ awk '/SUCCESS/' ansible.log| wc -l
66









share|improve this question















marked as duplicate by G-Man, telcoM, Jeff Schaller, n.st, RalfFriedl Dec 9 '18 at 18:37


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




















    4















    This question already has an answer here:




    • Count records matching pattern with Awk

      3 answers




    I have logs like as given sample below, where I'm simply looking for a SUCCESS string in the log file and counting the total.



    $ cat ansible.log
    lnx-host01.tin.com | SUCCESS => {"changed": false, "ping": "pong"}
    lnx-host02.tin.com | SUCCESS => {"changed": false, "ping": "pong"}


    This is what the simple straightforward way, but I'm wondering if it can be done with awk itself as a one liner without passing to the wc command.



    $ awk '/SUCCESS/{print $0}'  ansible.log | wc -l
    66

    OR

    $ awk '/SUCCESS/' ansible.log| wc -l
    66









    share|improve this question















    marked as duplicate by G-Man, telcoM, Jeff Schaller, n.st, RalfFriedl Dec 9 '18 at 18:37


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


















      4












      4








      4








      This question already has an answer here:




      • Count records matching pattern with Awk

        3 answers




      I have logs like as given sample below, where I'm simply looking for a SUCCESS string in the log file and counting the total.



      $ cat ansible.log
      lnx-host01.tin.com | SUCCESS => {"changed": false, "ping": "pong"}
      lnx-host02.tin.com | SUCCESS => {"changed": false, "ping": "pong"}


      This is what the simple straightforward way, but I'm wondering if it can be done with awk itself as a one liner without passing to the wc command.



      $ awk '/SUCCESS/{print $0}'  ansible.log | wc -l
      66

      OR

      $ awk '/SUCCESS/' ansible.log| wc -l
      66









      share|improve this question
















      This question already has an answer here:




      • Count records matching pattern with Awk

        3 answers




      I have logs like as given sample below, where I'm simply looking for a SUCCESS string in the log file and counting the total.



      $ cat ansible.log
      lnx-host01.tin.com | SUCCESS => {"changed": false, "ping": "pong"}
      lnx-host02.tin.com | SUCCESS => {"changed": false, "ping": "pong"}


      This is what the simple straightforward way, but I'm wondering if it can be done with awk itself as a one liner without passing to the wc command.



      $ awk '/SUCCESS/{print $0}'  ansible.log | wc -l
      66

      OR

      $ awk '/SUCCESS/' ansible.log| wc -l
      66




      This question already has an answer here:




      • Count records matching pattern with Awk

        3 answers








      awk






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 9 '18 at 14:26









      Jeff Schaller

      38.9k1053125




      38.9k1053125










      asked Dec 8 '18 at 14:32









      krock1516

      19619




      19619




      marked as duplicate by G-Man, telcoM, Jeff Schaller, n.st, RalfFriedl Dec 9 '18 at 18:37


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






      marked as duplicate by G-Man, telcoM, Jeff Schaller, n.st, RalfFriedl Dec 9 '18 at 18:37


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
























          1 Answer
          1






          active

          oldest

          votes


















          5














          Here it is:



          awk 'BEGIN { count=0 } /SUCCESS/ { count++ } END { print count }' ansible.log


          Of course this would have also worked:



          grep -c SUCCESS ansible.log





          share|improve this answer





















          • +1 for short grep solution (was going to post it but you were faster)
            – RomanPerekhrest
            Dec 8 '18 at 14:44












          • @ A.B , thnx , how about awk '/SUCCESS/ {count++} END{print count}' ansible.log
            – krock1516
            Dec 8 '18 at 14:58






          • 1




            Works almost fine too. It will print an empty line instead of 0 in case of no match (then count is considered by default an empty string). So if you can garantee there's always at least one SUCCESS then it's good enough. We're not on Code Golf heh.
            – A.B
            Dec 8 '18 at 15:06












          • That's fine @A.B :-) , indeed we are not in Code Golf .
            – krock1516
            Dec 8 '18 at 15:14






          • 1




            @krock1516 change print count to print count+0 and you will be fine.
            – mosvy
            Dec 8 '18 at 20:07


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          5














          Here it is:



          awk 'BEGIN { count=0 } /SUCCESS/ { count++ } END { print count }' ansible.log


          Of course this would have also worked:



          grep -c SUCCESS ansible.log





          share|improve this answer





















          • +1 for short grep solution (was going to post it but you were faster)
            – RomanPerekhrest
            Dec 8 '18 at 14:44












          • @ A.B , thnx , how about awk '/SUCCESS/ {count++} END{print count}' ansible.log
            – krock1516
            Dec 8 '18 at 14:58






          • 1




            Works almost fine too. It will print an empty line instead of 0 in case of no match (then count is considered by default an empty string). So if you can garantee there's always at least one SUCCESS then it's good enough. We're not on Code Golf heh.
            – A.B
            Dec 8 '18 at 15:06












          • That's fine @A.B :-) , indeed we are not in Code Golf .
            – krock1516
            Dec 8 '18 at 15:14






          • 1




            @krock1516 change print count to print count+0 and you will be fine.
            – mosvy
            Dec 8 '18 at 20:07
















          5














          Here it is:



          awk 'BEGIN { count=0 } /SUCCESS/ { count++ } END { print count }' ansible.log


          Of course this would have also worked:



          grep -c SUCCESS ansible.log





          share|improve this answer





















          • +1 for short grep solution (was going to post it but you were faster)
            – RomanPerekhrest
            Dec 8 '18 at 14:44












          • @ A.B , thnx , how about awk '/SUCCESS/ {count++} END{print count}' ansible.log
            – krock1516
            Dec 8 '18 at 14:58






          • 1




            Works almost fine too. It will print an empty line instead of 0 in case of no match (then count is considered by default an empty string). So if you can garantee there's always at least one SUCCESS then it's good enough. We're not on Code Golf heh.
            – A.B
            Dec 8 '18 at 15:06












          • That's fine @A.B :-) , indeed we are not in Code Golf .
            – krock1516
            Dec 8 '18 at 15:14






          • 1




            @krock1516 change print count to print count+0 and you will be fine.
            – mosvy
            Dec 8 '18 at 20:07














          5












          5








          5






          Here it is:



          awk 'BEGIN { count=0 } /SUCCESS/ { count++ } END { print count }' ansible.log


          Of course this would have also worked:



          grep -c SUCCESS ansible.log





          share|improve this answer












          Here it is:



          awk 'BEGIN { count=0 } /SUCCESS/ { count++ } END { print count }' ansible.log


          Of course this would have also worked:



          grep -c SUCCESS ansible.log






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 8 '18 at 14:42









          A.B

          4,0071724




          4,0071724












          • +1 for short grep solution (was going to post it but you were faster)
            – RomanPerekhrest
            Dec 8 '18 at 14:44












          • @ A.B , thnx , how about awk '/SUCCESS/ {count++} END{print count}' ansible.log
            – krock1516
            Dec 8 '18 at 14:58






          • 1




            Works almost fine too. It will print an empty line instead of 0 in case of no match (then count is considered by default an empty string). So if you can garantee there's always at least one SUCCESS then it's good enough. We're not on Code Golf heh.
            – A.B
            Dec 8 '18 at 15:06












          • That's fine @A.B :-) , indeed we are not in Code Golf .
            – krock1516
            Dec 8 '18 at 15:14






          • 1




            @krock1516 change print count to print count+0 and you will be fine.
            – mosvy
            Dec 8 '18 at 20:07


















          • +1 for short grep solution (was going to post it but you were faster)
            – RomanPerekhrest
            Dec 8 '18 at 14:44












          • @ A.B , thnx , how about awk '/SUCCESS/ {count++} END{print count}' ansible.log
            – krock1516
            Dec 8 '18 at 14:58






          • 1




            Works almost fine too. It will print an empty line instead of 0 in case of no match (then count is considered by default an empty string). So if you can garantee there's always at least one SUCCESS then it's good enough. We're not on Code Golf heh.
            – A.B
            Dec 8 '18 at 15:06












          • That's fine @A.B :-) , indeed we are not in Code Golf .
            – krock1516
            Dec 8 '18 at 15:14






          • 1




            @krock1516 change print count to print count+0 and you will be fine.
            – mosvy
            Dec 8 '18 at 20:07
















          +1 for short grep solution (was going to post it but you were faster)
          – RomanPerekhrest
          Dec 8 '18 at 14:44






          +1 for short grep solution (was going to post it but you were faster)
          – RomanPerekhrest
          Dec 8 '18 at 14:44














          @ A.B , thnx , how about awk '/SUCCESS/ {count++} END{print count}' ansible.log
          – krock1516
          Dec 8 '18 at 14:58




          @ A.B , thnx , how about awk '/SUCCESS/ {count++} END{print count}' ansible.log
          – krock1516
          Dec 8 '18 at 14:58




          1




          1




          Works almost fine too. It will print an empty line instead of 0 in case of no match (then count is considered by default an empty string). So if you can garantee there's always at least one SUCCESS then it's good enough. We're not on Code Golf heh.
          – A.B
          Dec 8 '18 at 15:06






          Works almost fine too. It will print an empty line instead of 0 in case of no match (then count is considered by default an empty string). So if you can garantee there's always at least one SUCCESS then it's good enough. We're not on Code Golf heh.
          – A.B
          Dec 8 '18 at 15:06














          That's fine @A.B :-) , indeed we are not in Code Golf .
          – krock1516
          Dec 8 '18 at 15:14




          That's fine @A.B :-) , indeed we are not in Code Golf .
          – krock1516
          Dec 8 '18 at 15:14




          1




          1




          @krock1516 change print count to print count+0 and you will be fine.
          – mosvy
          Dec 8 '18 at 20:07




          @krock1516 change print count to print count+0 and you will be fine.
          – mosvy
          Dec 8 '18 at 20:07



          Popular posts from this blog

          Сан-Квентин

          8-я гвардейская общевойсковая армия

          Алькесар