Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mikewallace1979
Created October 29, 2012 11:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikewallace1979/3973059 to your computer and use it in GitHub Desktop.
Save mikewallace1979/3973059 to your computer and use it in GitHub Desktop.
Basic stats from stdin
#!/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