The Smell of Molten Projects in the Morning

Ed Nisley's Blog: Shop notes, electronics, firmware, machinery, 3D printing, laser cuttery, and curiosities. Contents: 100% human thinking, 0% AI slop.

Day: March 8, 2012

  • Bash File Name Chopping for Gnuplot

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

    PI-Loop-ErrDrive
    PI-Loop-ErrDrive

    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!