awk to search the string and count for total in a one liner [duplicate]
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
awk
marked as duplicate by G-Man, telcoM, Jeff Schaller, n.st, RalfFriedl Dec 9 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.
add a comment |
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
awk
marked as duplicate by G-Man, telcoM, Jeff Schaller, n.st, RalfFriedl Dec 9 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.
add a comment |
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
awk
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
awk
edited Dec 9 at 14:26
Jeff Schaller
38.6k1053125
38.6k1053125
asked Dec 8 at 14:32
krock1516
19619
19619
marked as duplicate by G-Man, telcoM, Jeff Schaller, n.st, RalfFriedl Dec 9 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 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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
+1 for shortgrep
solution (was going to post it but you were faster)
– RomanPerekhrest
Dec 8 at 14:44
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
Dec 8 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 oneSUCCESS
then it's good enough. We're not on Code Golf heh.
– A.B
Dec 8 at 15:06
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
Dec 8 at 15:14
1
@krock1516 changeprint count
toprint count+0
and you will be fine.
– mosvy
Dec 8 at 20:07
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
+1 for shortgrep
solution (was going to post it but you were faster)
– RomanPerekhrest
Dec 8 at 14:44
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
Dec 8 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 oneSUCCESS
then it's good enough. We're not on Code Golf heh.
– A.B
Dec 8 at 15:06
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
Dec 8 at 15:14
1
@krock1516 changeprint count
toprint count+0
and you will be fine.
– mosvy
Dec 8 at 20:07
|
show 2 more comments
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
+1 for shortgrep
solution (was going to post it but you were faster)
– RomanPerekhrest
Dec 8 at 14:44
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
Dec 8 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 oneSUCCESS
then it's good enough. We're not on Code Golf heh.
– A.B
Dec 8 at 15:06
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
Dec 8 at 15:14
1
@krock1516 changeprint count
toprint count+0
and you will be fine.
– mosvy
Dec 8 at 20:07
|
show 2 more comments
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
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
answered Dec 8 at 14:42
A.B
3,9971724
3,9971724
+1 for shortgrep
solution (was going to post it but you were faster)
– RomanPerekhrest
Dec 8 at 14:44
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
Dec 8 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 oneSUCCESS
then it's good enough. We're not on Code Golf heh.
– A.B
Dec 8 at 15:06
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
Dec 8 at 15:14
1
@krock1516 changeprint count
toprint count+0
and you will be fine.
– mosvy
Dec 8 at 20:07
|
show 2 more comments
+1 for shortgrep
solution (was going to post it but you were faster)
– RomanPerekhrest
Dec 8 at 14:44
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
Dec 8 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 oneSUCCESS
then it's good enough. We're not on Code Golf heh.
– A.B
Dec 8 at 15:06
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
Dec 8 at 15:14
1
@krock1516 changeprint count
toprint count+0
and you will be fine.
– mosvy
Dec 8 at 20:07
+1 for short
grep
solution (was going to post it but you were faster)– RomanPerekhrest
Dec 8 at 14:44
+1 for short
grep
solution (was going to post it but you were faster)– RomanPerekhrest
Dec 8 at 14:44
@ A.B , thnx , how about
awk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
Dec 8 at 14:58
@ A.B , thnx , how about
awk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
Dec 8 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 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 at 15:06
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
Dec 8 at 15:14
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
Dec 8 at 15:14
1
1
@krock1516 change
print count
to print count+0
and you will be fine.– mosvy
Dec 8 at 20:07
@krock1516 change
print count
to print count+0
and you will be fine.– mosvy
Dec 8 at 20:07
|
show 2 more comments