Friday, October 7, 2022

[SOLVED] sum up results of awk and grep

Issue

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.


Solution

Following awk may help you on same:

awk '/money/{sum+=$3} END{print sum}' Input_file


Answered By - RavinderSingh13
Answer Checked By - Senaida (WPSolving Volunteer)