Archive for the ‘voodoo’ Category

Averaging text files with bash and awk

Friday, March 19th, 2010

Small script that might be useful for analysing data. The script takes multiple files (filemask is $1) which are composed of data columns, and produces the average of each data point across the files. Leaving around for future reference.

Usage: $ average.sh “filename.*” > filename.avg

sum=`ls -l $1 | wc -l`
tf=`ls $1 | tail -n 1`
fld=`tail -n 1 $tf | wc -w`

count=1
while [ $count -lt $(($fld + 1)) ]; do
paste -d” ” $1 | nawk -v s=”$sum”\
-v fld=”$fld” -v f=”$count” ‘{
for(i=0;i< =s-1;i++)
{
ta=f + i*fld
tta=tta+$ta
}
print tta/s
tta=0
}' >> tmp$count
count=$(($count + 1))
done
paste -d” ” tmp*
rm tmp*

Bonus points for anyone who can make the above code simpler.

UPDATE: Thanks Pat for the bugfix. Whups.

  • Categories

  • Archives

  • Meta


  • "A computer once beat me at chess, but it was no match for me at kick boxing."
    Unknown