Bash File Name Chopping for Gnuplot

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:

export GDFONTPATH="/usr/share/fonts/truetype/"
echo Base name: ${base}
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"

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


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!

About these ads
  1. #1 by smellsofbikes on 8-March-2012 - 11:59

    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.

    • #2 by Ed on 8-March-2012 - 15:06


      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.


      • #3 by smellsofbikes on 8-March-2012 - 15:12

        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.

        • #4 by Ed on 8-March-2012 - 18:10

          Many eyes make all bugs shallow!

          posting so much useful information

          I recommend a “trust but verify” approach… [grin]

  2. #5 by Fede Heinz on 8-March-2012 - 15:02

    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%.*}”‘

    • #6 by Ed on 8-March-2012 - 15:18

      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]

  3. #7 by Michael E Brown on 13-March-2012 - 00:25

    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.

    • #8 by Ed on 13-March-2012 - 07:35

      man bash, then search for ${

      Absolutely… the (well, my) problem: using them not quite often enough to remain fluent!