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.