Thursday, May 8, 2008

My kick-ass script to compute min,max,avg & standard deviation ;)


#!/bin/bash

function usage()
{
echo
echo "[*] usage: $0 filename column"
echo
exit
}

if [ -z $1 ] || [ -z $2 ]
then
usage
fi

line=`cat $1 | wc -l`
max=`cat $1 | awk -v c=$2 '{if($c>=s){s=$c;line=NR}}END{print s,"("line")"}'`
min=`cat $1 | awk -v s=9999999999 -v c=$2 '{if($c<=s){s=$c;line=NR};}END{print s,"("line")"}'`
avg=`cat $1 | awk -v c=$2 '{total=total+$c}END{print total/NR}'`
stdev=`cat $1 | awk -v avg=$avg '{x=$1-avg;total=total+x*x;}END{print sqrt(total/NR)}'`

echo
echo "[*] input : $1"
echo "[*] column : $2"
echo "[*] no of data : $line"

echo
echo "[*] min : $min"
echo "[*] max : $max"
echo "[*] std.dev : $stdev"
echo