Just so I can remember it for next time, this plot:

Came from a dataset with a zillion lines like this:
#Set Temp TZone TErr Int PDrive sPWM Time 30.0 15.7 3 -14.30 0.000 -1.000 -255 0 30.0 15.7 3 -14.30 0.000 -1.000 -255 142 30.0 15.7 3 -14.30 0.000 -1.000 -255 245 30.0 15.7 3 -14.30 0.000 -1.000 -255 348
Using this Bash script to allow many different file names:
#!/bin/sh
export GDFONTPATH="/usr/share/fonts/truetype/"
base=${1%%.*}
echo Base name: ${base}
ofile=${base}.png
echo Output file: ${ofile}
gnuplot << EOF
#set term x11
set term png font "arialbd.ttf" 18 size 950,600
set output "${ofile}"
set title "Peltier Test - Loop Tuning"
set key noautotitles
unset mouse
set bmargin 4
set grid xtics ytics
set xlabel "Time - sec"
#set format x "%4.0f"
#set xrange [5000:7500]
#set xtics 0,5
set mxtics 2
set ytics nomirror autofreq
set ylabel "Various"
set format y "%5.1f"
set yrange [-2:2]
#set mytics 2
#set y2label "PWM"
#set format y2 "%3.0f"
#set y2range [0:255]
#set y2tics 32
#set rmargin 9
set datafile separator "\t"
#set label 1 "HP + LP" at 0.25,-14 font "arialbd,14"
plot \
"$1" using (\$8/1000):4 with lines lt 3 title "Error" ,\
"$1" using (\$8/1000):6 with lines lt 4 title "Drive"
# "$1" using 4 with lines lt 3 title "Error" ,\
# "$1" using 6 with lines lt 4 title "Drive"
# "$1" using (\$8/1000):1 with lines lt 3 title "Setpoint" ,\
# "$1" using (\$8/1000):2 with lines lt 4 title "Temp C"
EOF
There’s quite some other cruft in there, but the first part I must remember is right up at the top, where the magic incantation
base=${1%%.*}
chops off the file extension. Of course, that doesn’t work worth beans when the file name has several periods scattered through it.
The other part is at the bottom, where various alternate lines for the plot command must live after the last valid parameter line: the octothorpe comment header doesn’t work inside a command!
Comments
8 responses to “Bash File Name Chopping for Gnuplot”
The original magic incantation line makes sense, your version down below ends in a bracket rather than a french brace.
Well, okay, the original makes my head hurt, but it parses better.
Fixed…
The WordPress source code markup handler occasionally translates “special characters” into their equivalent HTML escaped-characters, but that one looks like a straightforward finger fumble.
Thanks!
I can proofread even if I don’t understand what you’re doing. Although with that said, I’m suddenly in need of a peltier controller, so I may be copying what you’re doing right soonish: thanks again for posting so much useful information on all sorts of topics.
Many eyes make all bugs shallow!
I recommend a “trust but verify” approach… [grin]
There’s a typo there. You put a ‘]’ instead of ‘}’
Also, as a tip, ‘base=${1%.*}’ does the trick, even with multiple dots, because it is non-greedy. I suggest you put the variable expansion in double quotes, so it will work even with filenames that contain spaces and tabs: ‘base=”${1%.*}”‘
Just fixed the typo… glad you’re watching!
I need to use that stuff just often enough that I remember having once known how it worked, but not often enough that I can actually write it with any fluency. [sigh]
man bash, then search for ${
There is a little section with all those incantations. Very handy. The #, ##, %, and %% are all documented there, and all very handy.
Absolutely… the (well, my) problem: using them not quite often enough to remain fluent!