awk to search the string and count for total in a one liner [duplicate]
up vote
4
down vote
favorite
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 yesterday
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 |
up vote
4
down vote
favorite
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 yesterday
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 |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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 yesterday
Jeff Schaller
37.5k1052121
37.5k1052121
asked 2 days ago
krock1516
19619
19619
marked as duplicate by G-Man, telcoM, Jeff Schaller, n.st, RalfFriedl yesterday
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 yesterday
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
up vote
5
down vote
accepted
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 shortgrepsolution (was going to post it but you were faster)
– RomanPerekhrest
2 days ago
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
2 days ago
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 oneSUCCESSthen it's good enough. We're not on Code Golf heh.
– A.B
2 days ago
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
2 days ago
1
@krock1516 changeprint counttoprint count+0and you will be fine.
– mosvy
2 days ago
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
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 shortgrepsolution (was going to post it but you were faster)
– RomanPerekhrest
2 days ago
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
2 days ago
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 oneSUCCESSthen it's good enough. We're not on Code Golf heh.
– A.B
2 days ago
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
2 days ago
1
@krock1516 changeprint counttoprint count+0and you will be fine.
– mosvy
2 days ago
|
show 2 more comments
up vote
5
down vote
accepted
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 shortgrepsolution (was going to post it but you were faster)
– RomanPerekhrest
2 days ago
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
2 days ago
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 oneSUCCESSthen it's good enough. We're not on Code Golf heh.
– A.B
2 days ago
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
2 days ago
1
@krock1516 changeprint counttoprint count+0and you will be fine.
– mosvy
2 days ago
|
show 2 more comments
up vote
5
down vote
accepted
up vote
5
down vote
accepted
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 2 days ago
A.B
3,7571723
3,7571723
+1 for shortgrepsolution (was going to post it but you were faster)
– RomanPerekhrest
2 days ago
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
2 days ago
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 oneSUCCESSthen it's good enough. We're not on Code Golf heh.
– A.B
2 days ago
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
2 days ago
1
@krock1516 changeprint counttoprint count+0and you will be fine.
– mosvy
2 days ago
|
show 2 more comments
+1 for shortgrepsolution (was going to post it but you were faster)
– RomanPerekhrest
2 days ago
@ A.B , thnx , how aboutawk '/SUCCESS/ {count++} END{print count}' ansible.log
– krock1516
2 days ago
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 oneSUCCESSthen it's good enough. We're not on Code Golf heh.
– A.B
2 days ago
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
2 days ago
1
@krock1516 changeprint counttoprint count+0and you will be fine.
– mosvy
2 days ago
+1 for short
grep solution (was going to post it but you were faster)– RomanPerekhrest
2 days ago
+1 for short
grep solution (was going to post it but you were faster)– RomanPerekhrest
2 days ago
@ A.B , thnx , how about
awk '/SUCCESS/ {count++} END{print count}' ansible.log– krock1516
2 days ago
@ A.B , thnx , how about
awk '/SUCCESS/ {count++} END{print count}' ansible.log– krock1516
2 days ago
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
2 days ago
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
2 days ago
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
2 days ago
That's fine @A.B :-) , indeed we are not in Code Golf .
– krock1516
2 days ago
1
1
@krock1516 change
print count to print count+0 and you will be fine.– mosvy
2 days ago
@krock1516 change
print count to print count+0 and you will be fine.– mosvy
2 days ago
|
show 2 more comments