Scripts for computing the average of a list of numbers in a data file
Here is one method:
$ awk '{s+=$1}END{print "ave:",s/NR}' RS=" " file
ave: 54.646
Another option is to use jq
:
$ seq 100|jq -s add/length
50.5
-s
(--slurp
) creates an array for the input lines after parsing each line as JSON, or as a number in this case.
Or in the OP's case:
tr \ \\n<file|jq -s add/length|sed s/^/ave:\ /
perl -lane '$a+=$_ for(@F);print "ave: ".$a/scalar(@F)' file
if you have multiple lines and you just need a single average:
perl -lane '$a+=$_ for(@F);$f+=scalar(@F);END{print "ave: ".$a/$f}' file