From: William James Date: 2006-12-02T19:45:08+09:00 Subject: Re: calculations on lists of numbers ara.t.howard@noaa.gov wrote: > for years i've felt that i should be able to pipe numerical output into some > unix command like so > > cat list | mean > cat list | sum > cat list | minmax Why drag in the cat when it's utterly superfluous? mean > etc. > > and have never found one. right now i'm building a ruby version - before i > continue, does anyone know a standard unix or ruby version of this? Matthew Moss wrote: > > cat list | mean > > cat list | awk '{ s += $1; n += 1 } END { print s / n }' Reading a file isn't a magical ability; awk can do it. awk "{s+=$0} END{print s/NR}" list If there can be more than one number on a line: awk "{for(i=1;i<=NF;i++)s+=$i; n+=NF} END{print s/n}" file Ruby: ruby -nale "BEGIN{$s=$n=0}; $s+=$F.inject(0){|x,y| x.to_f+y.to_f}; $n+=$F.size; END{puts $s/$n}" file If there's memory enough for the whole file: ruby -e "a=$<.read.split.map{|x|x.to_f}; puts a.inject{|x,y|x+y}/a.size" file