Blog / Scripting

Tuesday 05 August 2008

Using awk to calculate averages

Here's a simple little awk script, averages.awk that takes a datafile and returns the average value of each column in that file:-

{ for (i = 1; i <= NF; i++) x[i]+=$i ; j++ }
END { for ( i=1 ; i<= NF ; i++ ) print x[i]/j }

The first line of the script goes through the datafile keeping a tally of each column in the array x; the number of iterations is stored in the variable j. The variable NF contains the number of columns read in from the file by awk. The second line is executed at the end and returns the mean average of each column.

The script is simply called from the command line with the last parameter being the name of the datafile from which the averages are to be calculated:-

> awk -f 'averages.awk' datafile.dat

— Andy

Posted in:

Comment

Wednesday 23 July 2008

Automating Gnuplot with scripting

I have recently found myself with a large number of data files that all wanted to be fed through Gnuplot to produce a number of figures. Not wanting to spend ages manually feeding them in to Gnuplot I decided to have a go at scripting something to save myself a lot of time. I hope that sharing this here is of use to somebody out there.

The script, script.sh:-

#! /bin/bash
ls *.data | sed "s/.data//" > list
for i in `cat list` ; do
   sed -e "s/INPUTFILE/$i/" -e "s/OUTPUTFILE/$i/" \
    plot.gnu | gnuplot
done
rm list

The script, written in Bash, creates a file called list in which all the data files matching the condition *.data are listed (stripped of their file extension .data). It then reads through this file invoking sed to replace INPUTFILE and OUTPUTFILE in the Gnuplot script plot.gnu (see below) with the name of the data file and passes this to Gnuplot. Finally, the temporary file list is removed. Simple!

Remember the script needs to be executable: chmod +x script.sh

The Gnuplot file, plot.gnu:-

set term postscript enhanced color
set output 'OUTPUTFILE.eps'
set title 'OUTPUTFILE'
p 'INPUTFILE.data' u 1:2 w l

Obviously you can make the Gnuplot file as advanced as you like to produce the sort of figures you desire with your data.

— Andy

Posted in:

Comment

Recent blog posts

Links