calculate sum and average of lines of number

Assuming a file contains a bunch of numbers, one number per line, like this:

1
2
3
4
5
6

One can easily calculate the sum and average of all these numbers, by using awk:

cat numbers | awk '{sum += $1; count += 1} END {print "sum:" sum " avg:" sum/count}'

 

Leave a Reply

Your email address will not be published. Required fields are marked *