Last active
April 27, 2016 21:54
-
-
Save jalex19100/aa53d46332e2f80e33138d0443c557d1 to your computer and use it in GitHub Desktop.
The above script reads from stdin, and prints tab-separated columns of output on a single line. See: http://unix.stackexchange.com/questions/13731/is-there-a-way-to-get-the-min-max-median-and-average-of-a-list-of-numbers-in
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
sort -n | awk ' | |
BEGIN { | |
c = 0; | |
sum = 0; | |
} | |
$1 ~ /^[0-9]*(\.[0-9]*)?$/ { | |
a[c++] = $1; | |
sum += $1; | |
} | |
END { | |
ave = sum / c; | |
if( (c % 2) == 1 ) { | |
median = a[ int(c/2) ]; | |
} else { | |
median = ( a[c/2] + a[c/2-1] ) / 2; | |
} | |
OFS="\t"; | |
print "sum, count, avg, median, min, max" | |
print sum, c, ave, median, a[0], a[c-1]; | |
} | |
' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment