Call Python and Gnuplot in bash script
Here I show a basic example of how to include Python and Gnuplot scripts within a bash script. I find it useful when I want to do the same operation on several files in the same folder. Say you have several ascii files *.csv, this script first does some operations on the file, than calls a python script, and finally a Gnuplot script.
#!/bin/bash
for f in *.csv # for all files ending with '*.csv'
do
echo 'Processing' $f # print on screen
cp $f $f.dat # copy the file to a temporary file .csv.dat
sed -i '1d' $f.dat # remove the header (first line)
sed 's/,/ /g' $f.dat > $f.txt # replace comma with blank space (this in a new file .txt)
#
# Python commands start here
python - <<END
import numpy as np
A = np.genfromtxt('$f.txt') # recover data from $f.txt
for i in range(0,len(A)): # do some operations ...
if A[i][4]<0.5:
A[i][5]=0.
np.savetxt('$f.dat', A) # here I save A in a file .csv.dat
END
# End of python commands
#
sed '0~200 a\\' $f.dat > $f.txt # every 200 lines leave a blank line (this in a new file .txt)
rm $f.dat # rm the temporary file .dat, so remain with original .csv and final .txt
# Gnuplot commands start here
gnuplot - <<- EOF
set terminal epscairo enhanced
set output '$f.eps'
set xlabel 'x/L' # do whatever you want
set ylabel 'z/W'
plot './$f.txt' u (\$6/0.1):(\$7/0.08) w l notitle # note that you need to use \$ to call the line to plot
# otherwise you get confusion with the bash variable
EOF
# End of gnuplot commands
done