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
# 8  
Old 07-11-2013
To print the null outputs, you need the other approach, but add a 0 to force a number.
Code:
awk '/=\+/ { okcount++ } /=\-/ { errcount++ } /=\0/ { waitcount++ } /=\?/ { runcount++ }
END { print "Status: " okcount+0 " OK, " errcount+0 " ERR, " waitcount+0 " WAITING, " runcount+0 " RUNNING"}'

This User Gave Thanks to MadeInGermany For This Post:
# 9  
Old 07-11-2013
Fixed the awk statement, but it's still not showing the desired results Smilie

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

stdout:
Code:
Status: 216 WAITING, 0 RUNNING, 215 OK, 0 ERROR

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

---------- Post updated at 09:37 AM ---------- Previous update was at 09:22 AM ----------

Thanks to all again.. finally got it to work by removing the backslash before "0" and used this awk statement:

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

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