Sum the values in a column except the header
awk '{s+=$3}END{print s}' yourfile
Pretty trivial using just awk
. Assuming the example data is in a file, ex.txt
:
$ awk '{total = total + int($3)}END{print "Total Amount collected = "total}' ex.txt
Example
$ awk '{total = total + $3}END{print "Total Amount collected = "total}' ex.txt
Total Amount collected = 95657
Details
Using awk
we collect the values from the 3rd column ($3
) and accumulate their sub-total in the variable total
. Once complete, as the last thing to do, END{..}
, we print the message along with the value of the variable total
.
total=0;
for n in $( tail -n +4 /tmp/reports.txt | awk '{print $3}') ;
do
total=$( expr $total + $n );
done ;
echo ">>$total"