Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the quickest way to get the mean of a set of numbers from the command line?

Using any tools which you would expect to find on a nix system (in fact, if you want, msdos is also fine too), what is the easiest/fastest way to calculate the mean of a set of numbers, assuming you have them one per line in a stream or file?

like image 510
Anthony Avatar asked Oct 18 '08 01:10

Anthony


4 Answers

Using "st" (https://github.com/nferraz/st):

$ st numbers.txt
N      min   max    sum    mean  sd
10.00  1.00  10.00  55.00  5.50  3.03

Specify an option to see individual stats:

$ st numbers.txt --mean
5.5

(DISCLAIMER: I wrote this tool :))

like image 134
user2747481 Avatar answered Oct 26 '22 18:10

user2747481


Awk

awk '{total += $1; count++ } END {print total/count}'
like image 25
Andru Luvisi Avatar answered Oct 26 '22 20:10

Andru Luvisi


awk ' { n += $1 }; END { print n / NR }'

This accumulates the sum in n, then divides by the number of items (NR = Number of Records).

Works for integers or reals.

like image 33
Adam Liss Avatar answered Oct 26 '22 19:10

Adam Liss


Using Num-Utils for UNIX:

average 1 2 3 4 5 6 7 8 9
like image 40
Mitch Wheat Avatar answered Oct 26 '22 19:10

Mitch Wheat