Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum up results of awk and grep

Tags:

grep

awk

I have put together a little chain of grep and awk commands that return me a column of floating point numbers and I would like to know their sum, so something like:

cat file | grep 'money' | awk '{print $3}' | grep -v 'nonsense'
1.23
4.56
7.8
cat file | grep 'money' | awk '{print $3}' | grep -v 'nonsense' | sum
13.68

Does anybody know a way to do that ?


Edit: The example given is just a simplification of my actual command, so my question is not a duplicate of the link provided, since the solution given there is not really applicable to my problem. Also the title of the question does not describe the problem I have, therefore I couldn't find it. RavinderSingh13's answer is nice, precise and simple enough to be adaptable to my problem. Thanks again.

like image 974
Samarek Avatar asked Oct 15 '25 03:10

Samarek


1 Answers

Following awk may help you on same:

awk '/money/{sum+=$3} END{print sum}' Input_file
like image 143
RavinderSingh13 Avatar answered Oct 18 '25 10:10

RavinderSingh13



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!