#! /bin/bash
#
# stats - read numbers from std-in, one per line, and output to std-out some
#   simple statistics on the values read.  Example use:
#
#     j=1 ; while [ $j -lt 11 ] ; do echo $RANDOM ; (( j++ )) ; done | stats

gawk 'BEGIN {
	min = 0
	max = 0
	total = 0
	if (getline > 0) {
	  min = $0
	  max = $0
	  total = $0
	  samples[NR] = $0
	  }
	}

     { total += $0
       samples[NR] = $0
       if ($0 < min) min = $0
       else if (max < $0) max = $0
       }

     END {
       print "min = " min
       print "max = " max
       print "n = " NR

       avg = NR == 0 ? 0 : total/NR
       print "average = " avg

       stddev = 0
       if (NR > 1) {
	 for (i = 1; i <= NR; i++)
	   stddev += samples[i]*samples[i]
	 stddev = sqrt((stddev - NR*avg*avg)/(NR - 1))
	 }
       print "std dev = " stddev
       print "var = " stddev*stddev
       }
     '

# $Log: stats,v $
# Revision 1.3  2005/04/04 03:08:21  rclayton
# Print the variance too.
#
# Revision 1.2  2005/03/27 01:46:02  rclayton
# Use gawk for portability.
#


syntax highlighted by Code2HTML, v. 0.9