Created
October 29, 2012 11:30
-
-
Save mikewallace1979/3973059 to your computer and use it in GitHub Desktop.
Basic stats from stdin
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/bash | |
# Quick hacky script that computes basic statistics from a single | |
# column of numbers from stdin - inspired by the CouchDB _stats reducer | |
input=`cat` | |
sum=`echo "$input" | awk '{sum+=$1} END {print sum}'` | |
count=`echo "$input" | awk '{} END {print NR}'` | |
min=`echo "$input" | awk '{if (NR == 1 || $1 < min) min=$1} END {print min}'` | |
max=`echo "$input" | awk '{if (NR == 1 || $1 > max) max=$1} END {print max}'` | |
mean=`echo "$input" | awk '{sum+=$1; array[NR]=$1} END {print sum/NR}'` | |
# Stolen from http://www.commandlinefu.com/commands/view/1661/display-the-standard-deviation-of-a-column-of-numbers-with-awk | |
stddev=`echo "$input" | awk '{sum+=$1; array[NR]=$1} END {for(x=1;x<=NR;x++){sumsq+=((array[x]-(sum/NR))**2);}print sqrt(sumsq/NR)}'` | |
echo "sum: $sum" | |
echo "count: $count" | |
echo "min: $min" | |
echo "max: $max" | |
echo "mean: $mean" | |
echo "standard deviation: $stddev" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment