awk to sum specific field when pattern matches

Trying to sum field #6 when field #2 matches string as follows:

Input data:

2010-09-18-20.24.44.206117 UOWEXEC                db2bp      DB2XYZ               hostname                       1
2010-09-18-20.24.44.206117 UOWWAIT                db2bp      DB2XYZ               hostname                       1
2010-09-18-20.24.44.206117 UOWWAIT                disp+work  SAPR3                hostname                      31

awk command:

awk '{ if ( $2 == "UOWWAIT" ) { wait+=$6 ; } else { nowait+=$6 ; } { print wait " " nowait } }'

but the output is:

 1
1 1
32 1

Please advise on the awk syntax so that the output is only "32 1".

Thanking you in advance.

awk '{ if ( $2 == "UOWWAIT" ) { wait+=$6 ; } else { nowait+=$6 ; }} END{ print wait " " nowait }'
1 Like

Found the answer:

awk '{ if ( $2 == "UOWWAIT" ) { wait+=$6 ; } else { nowait+=$6 ; } } END { print wait " " nowait }'

---------- Post updated at 03:35 PM ---------- Previous update was at 03:35 PM ----------

[/COLOR]thanks Bartus11.. that was super quick!

---------- Post updated at 03:51 PM ---------- Previous update was at 03:35 PM ----------

now i m trying to take another step to ensure that the "wait" connection does not belong to SAPR3 userid. i tried this syntax:

awk '{ if ( $2 == "UOWWAIT" && $4 != "SAPR3" ) { wait+=$6 ; } else { nowait+=$6 ; } } END { print wait "_" nowait }'

output:

2 31

instead of:

1 31

Please advise on the syntax. Thanking you in advance.

awk '/UOWWAIT/ {if ($4 != "SAPR3" )  {wait+=$6}  else {nowait+=$6} } END { print wait "_" nowait }' infile