A quick little awk script to compute the average and standard deviation of a specified column in a file.
#! /bin/sh
# Bulletproofing
if [[ $# -lt 2 ]]; then
echo "Usage: ./avgStd.sh <file> <column>"
exit
fi
# Compute Average and Std. Dev.
avg=`awk -v var=$2 'BEGIN{count=0; avg=0; std=0} {count=count+1; avg=avg+$var} END{print avg/count}' $1`
std=`awk -v var=$2 -v av=$avg 'BEGIN{count=0; std=0} {std=std + ($var-av)*($var-av); count=count+1} END{print sqrt((std)/(count-1))}' $1`
# Print results
echo "Average:\t$avg"
echo "Std. Dev:\t$std"
You should be able to copy/paste this code to a file, run chmod o+x on the file and run the command with the two arguments, file and column. For example:
./avgStd.sh derp 2
No comments:
Post a Comment