awk to sum specific field when pattern matches


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to sum specific field when pattern matches
# 1  
Old 09-18-2010
awk to sum specific field when pattern matches

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

Input data:
Code:
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:
Code:
awk '{ if ( $2 == "UOWWAIT" ) { wait+=$6 ; } else { nowait+=$6 ; } { print wait " " nowait } }'

but the output is:
Code:
 1
1 1
32 1

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

Thanking you in advance.

Last edited by Scott; 09-18-2010 at 05:37 PM.. Reason: Please use CODE tags
# 2  
Old 09-18-2010
Code:
awk '{ if ( $2 == "UOWWAIT" ) { wait+=$6 ; } else { nowait+=$6 ; }} END{ print wait " " nowait }'

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 09-18-2010
Found the answer:
Code:
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 ----------

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:

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

output:
Code:
2 31

instead of:
Code:
1 31

Please advise on the syntax. Thanking you in advance.

Moderator's Comments:
Mod Comment Use code tags, please.

Last edited by Scott; 09-18-2010 at 05:57 PM.. Reason: Please use CODE tags
# 4  
Old 09-19-2010
Code:
awk '/UOWWAIT/ {if ($4 != "SAPR3" )  {wait+=$6}  else {nowait+=$6} } END { print wait "_" nowait }' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine matches and use a field to adjust coordinates in other fields

Trying to output a result that uses the data from file to combine and subtract specific lines. If $4 matches in each line then the last $6 value is added to $2 and that becomes the new$3. Each matching line in combined into one with $1 then the original $2 then the new$3 then $5. For the cases... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to remove lines in file if specific field matches

I am trying to remove lines in the target.txt file if $5 before the - in that file matches sorted_list. I have tried grep and awk. Thank you :). grep grep -v -F -f targets.bed sort_list grep -vFf sort_list targets awk awk -F, ' > FILENAME == ARGV {to_remove=1; next} > ! ($5 in... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk to output specific matches in file

Using the attached file, the below awk command results in the output below: I can not seem to produce the desired results and need some expert help. Thank you :). awk -F'' ' { id += $4 value += $5 occur++ } END{ printf "%-8s%8s%8s%8s\n", "Gene", "Targets", "Average Depth", "Average... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

awk with range but matches pattern

To match range, the command is: awk '/BEGIN/,/END/' but what I want is the range is printed only if there is additional pattern that matches in the range itself? maybe like this: awk '/BEGIN/,/END/ if only in that range there is /pattern/' Thanks (8 Replies)
Discussion started by: zorrox
8 Replies

5. Shell Programming and Scripting

Breaking the sum of a column when specific pattern is found

Hi Am trying to sum up a file # cat /Out maths 2 0.0 english 2091 0.2 history -1 0.0 physics 18 0.0 -------------------------------------- maths ... (9 Replies)
Discussion started by: Priya Amaresh
9 Replies

6. Shell Programming and Scripting

Displaying the first field if the second field matches the pattern using Perl

Hi, I am trying with the below Perl command to print the first field when the second field matches the given pattern: perl -lane 'open F, "< myfile"; for $i (<F>) {chomp $i; if ($F =~ /patt$/) {my $f = (split(" ", $i)); print "$f";}} close F' dummy_file I know I can achieve the same with the... (7 Replies)
Discussion started by: royalibrahim
7 Replies

7. Shell Programming and Scripting

NAWK to remove lines that matches a specific pattern

Hi, I have requirement that I need to split my input file into two files based on a search pattern "abc" For eg. my input file has below content abc defgh zyx I need file 1 with abc and file2 with defgh zyx I can use grep command to acheive this. But with grep I need... (8 Replies)
Discussion started by: sbhuvana20
8 Replies

8. Shell Programming and Scripting

Displaying lines of a file where the second field matches a pattern

Howdy. I know this is most likely possible using sed or awk or grep, most likely a combination of them together, but how would one go about running a grep like command on a file where you only try to match your pattern to the second field in a line, space delimited? Example: You are... (3 Replies)
Discussion started by: LordJezoX
3 Replies

9. Shell Programming and Scripting

Print line if first Field matches a pattern

Hi All, I would like my code to be able to print out the whole line if 1st field has a dot in the number. Sample input and expected output given below. My AWK code is below but it can;t work, can any expert help me ? Thanks in advance. {if ($1 ~ /*\.*/) { print $0 }} Input: ... (2 Replies)
Discussion started by: Raynon
2 Replies

10. Shell Programming and Scripting

awk to count pattern matches

i have an awk statement which i am using to count the number of occurences of the number ,5, in the file: awk '/,5,/ {count++}' TRY.txt | awk 'END { printf(" Total parts: %d",count)}' i know there is a total of 10 matches..what is wrong here? thanks (16 Replies)
Discussion started by: npatwardhan
16 Replies
Login or Register to Ask a Question