Combine 4 awk pattern count statements into 1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Combine 4 awk pattern count statements into 1
# 1  
Old 07-11-2013
Combine 4 awk pattern count statements into 1

Hello fellow awkers,

I am trying to combine the following awk statements into 1 so that the results are more accurate:

Code:
awk '/\=\+/ { count++ } END { print count}' filename
awk '/\=\?/ { count++ } END { print count}' filename
awk '/\=\-/ { count++ } END { print count}' filename
awk '/\=\0/ { count++ } END { print count}' filename

where pattern matching: + is OK, - is ERROR, 0 is WAITING, ? is RUNNING

And the desired output is:
Code:
Status: xxx RUNNING, yyy FINISHED, zzz ERROR, xyz WAITING

where xxx is RUNNING count, yyy is OK count, zzz is ERROR count, xyz is WAITING count

Please advise and thanking you in advance.
# 2  
Old 07-11-2013
Code:
awk '/\=\+/ { countplus++ } /\=\?/ { countqn++ } END { print "Status: " countplus " OK , " countqn " Running" }' filename

This User Gave Thanks to anbu23 For This Post:
# 3  
Old 07-11-2013
Thanks for the quick one-liner!

Code:
awk '/\=\+/ { okcount++ } /\=\-/ { errcount++ } /\=\0/ { waitcount++ } /\=\?/ { runcount++ } END { print "Status: " okcount " OK, " errcount " ERR, " waitcount " WAITING, " runcount " RUNNING"}'

Adapted your solution, and it works good except for when count=0, it shows NULL...
# 4  
Old 07-11-2013
A hash can save you specifying each item twice (once for the variable and once for the printout).
Code:
awk '
/=\+/ { C["OK"]++ }
/=\?/ { C["RUNNING"]++ }
/=\-/ { C["ERROR"]++ }
/=\0/ { C["WAITUNG"]++ }
END {
printf "Status: "
for (i in C) { printf sep C[i] " " i; sep=", " }
print ""
}' filename

This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 07-11-2013
Thanks.. somehow I am getting parsing error when i have this:

Code:
awk '
/\=\+/ { C["OK"]++ }
/\=\?/ { C["RUNNING"]++ }
/\=\-/ { C["ERROR"]++ }
/\=\0/ { C["WAITING"]++ }
END {
printf "Status: "
for (i in C) { if(C[i]=="") {C[i]=0} ; {printf sep C[i] " " i; sep=", " }
print ""
}'

Looks like my if statement is incorrectly placed.. Any advice please? Thanks.
# 6  
Old 07-11-2013
you are missing one closing curly bracket at the end.
This User Gave Thanks to vidyadhar85 For This Post:
# 7  
Old 07-11-2013
Placed the curly bracket at the end and no parsing error now; however the expected results is not displayed.

stdout:
Code:
Status: 215 WAITING, 215 OK


expected:
Code:
Status: 215 OK, 0 RUNNING, 0 ERROR, 0 WAITING

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk with sed to combine lines and remove specific odd # pattern from line

In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Count lines with awk if statements

Hi Everybody, I wanna count lines in many files, but only if they meet a condition, I have something like this, cat /path1/usr/STAT/GPRS/ESTCOL_GPRS_2016* | awk 'BEGIN{FS=",";}{ if (substr($5,1,8)=='$DATE'){a++} END{for(i in a)print a}}' DATE=$(date +%Y%m%d -d "1 day ago") But it has... (6 Replies)
Discussion started by: Elly
6 Replies

3. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

4. Shell Programming and Scripting

Perl combine multiple map statements

I have a file like file. file.TODAY.THISYEAR file.TODAY.LASTYEARI want to substitute the words in caps with their actual values so that output should look like file.140805 file.140805.2014 file.140805.2013For this I am reading the file line bye line in an array and using multiple map... (1 Reply)
Discussion started by: sam05121988
1 Replies

5. UNIX for Dummies Questions & Answers

Combine two awk statements into one

Hi, I have the following two awk statements which I'd like to consolidate into one by piping the output from the first into the second awk statement (rather than having to write kat.txt out to a file and then reading back in). awk 'BEGIN {FS=OFS=" "} {printf("%s ", $2);for (x=7; x<=10;... (3 Replies)
Discussion started by: kasan0
3 Replies

6. Shell Programming and Scripting

simplify/combine if statements would be nice

below is something i inherited: if && && ; then HOST_SELECT="-m quadcore" fi if && && ; then HOST_SELECT="-m quadcore" fi if && && ; then HOST_SELECT="-m octocore1" fibelow is what i changed it to: if && && ; then HOST_SELECT="-m quadcore"... (2 Replies)
Discussion started by: crimso
2 Replies

7. UNIX for Dummies Questions & Answers

Struggling to combine two Greps statements

Greetings! I have been tasked to create a report off files we receive from our hardware suppliers. I need to grep these files for two fields 'Test_Version' and 'Model-Manufacturer' ; for each field, I need to capture their corresponding values. When running each statement separately, I get... (4 Replies)
Discussion started by: alan
4 Replies

8. Shell Programming and Scripting

Combine awk statements

I have an awk statement that works but I am calling awk twice and I know there has to be a way to combine the two statements into one. The purpose is to pull out just the ip address from loopback1. cat config.txt | nawk 'BEGIN {FS="\n"}{RS="!"}{if ( $0 ~ "interface loopback1" ) print$4}' | nawk... (5 Replies)
Discussion started by: numele
5 Replies

9. Shell Programming and Scripting

combine two grep statements

Hi I am wondering is it possible to combine two greps together I have two greps. grep "^,, *\." file (grep the line which has a '.' in the third column) grep "=" file (grep the line which has = anywhere) How to put them together so that if the content of the file that match either... (1 Reply)
Discussion started by: tiger66
1 Replies

10. UNIX for Dummies Questions & Answers

How to combine case statements

Hi, I need to change military time to regular time. I know to use case to indicate whether a.m. or p.m. as follows: case "$hour" in 0? | 1 ) echo a.m.;; 1 ) echo p.m.;; * ) echo p.m.;; esac My question is how do I add the hour and minute... (2 Replies)
Discussion started by: karp3158
2 Replies
Login or Register to Ask a Question