25 pts.
 Using awk to sum a field in a file
I am using the following command to sum the 3rd column in a ~ delimited file. awk -F~ '{total+=$3} END{print total}' /tmp/foo Can I add conditions to this command e.g. sum column 3 if column 5 greater than 160 and less than 171 etc? Thanks

Software/Hardware used:
ASKED: July 22, 2009  7:45 PM
UPDATED: August 4, 2009  8:07 PM

Answer Wiki:
<b><i>I think</i></b> you could use a conditional expression as in C. Something like this: <pre>awk -F~ '{total += $5 > 160 ? $3 : $2} END{print total}' /tmp/foo</pre> In this example, if $5 is greater than 160 it will sum $3, otherwise it will sum $2.
Last Wiki Answer Submitted:  July 29, 2009  9:29 pm  by  carlosdl   63,535 pts.
All Answer Wiki Contributors:  carlosdl   63,535 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

Thanks guys. I have tried this but does not work for me. I keep getting:

awk: syntax error near line 1
awk: illegal statement near line 1
awk: illegal statement near line 1

I have tried to change a few things here and there but no go.

Cheers

 25 pts.