command to replace specific contents of all *.gpl files - linux

I am having this below line in my *.gpl file .
#WANTS TO MODIFY REFERENCE_OUTPUT_* HERE IN BELOW LINES
set output "REFERENCE_OUTPUT_fun1.png"
set output "REFERENCE_OUTPUT_fun2.png"
set output "REFERENCE_OUTPUT_fun3.png"
set output "REFERENCE_OUTPUT_fun4.png"
-
-
-
#DO NOT WANTS TO MODIFY REFERENCE_OUTPUT_* FOR BELOW LINE
plot '/project/subfolder1/REFERENCE_OUTPUT_fun1.txt' u 1:2 w l axes x1y1 ti "Ref output" lc rgb "red"
and I do have minimum 800+ *.gpl files in dump folder .
I want to dump my generated output png files in separate images folder .
So,
I am trying below command to do so :
sed -i 's/set output "REFERENCE_OUTPUT_*/set output "./images/REFERENCE_OUTPUT_*/g' {} *.gpl
But getting Below Error
Error:
sed: -e expression #1, char 25: unknown option to `s'
Expected Output:
In all *.gpl files this above set output line changes from :
set output "REFERENCE_OUTPUT_fun1.png"
to
set output "./images/REFERENCE_OUTPUT_fun1.png"
UPDATE:
*.gpl file also has :
plot '/project/subfolder1/REFERENCE_OUTPUT_fun1.txt' u 1:2 w l axes x1y1 ti "Ref output" lc rgb "red"
So in above line REFERENCE_OUTPUT_fun1.txt also has name REFERENCE_OUTPUT_fun1 which I do not want to change . So that's the reason I am using set output "REFERENCE_OUTPUT_* which will just modify this set output . . line (in theory)

You don't need to match entire string - just replace prefixes
sed -E -i -u 's/(REFERENCE[a-zA-Z_0-9]+\.png)/\.\/images\/\1/g' *.gpl

Related

"x range is invalid" when using values with $

I'm on linux, learning gnuplot. My data.tsv looks like this:
1480420804 2016-11-29 04:00:04 -0800 foo1 $123.00 bar1
1480507205 2016-11-30 04:00:05 -0800 foo2 $124.25 bar2
1480593604 2016-12-01 04:00:04 -0800 foo3 $122.75 bar3
I'm using column #1 (seconds since epoch) as X, and column 4 (price) as Y.
This is my gnuplot script:
#!/usr/bin/gnuplot
set terminal png notransparent interlace size 640,480
set output "output.png"
set datafile separator tab
set xdata time
set timefmt "%s"
set format x "%4Y-%02m-%02d"
plot "data.tsv" using 1:4 title "Blah"
When I try to run this, I get the following error:
"./test.gp", line 9: warning: Skipping data file with no valid points
plot "data.tsv" using 1:4 title "Blah"
^
"./test.gp", line 9: x range is invalid
But if I remove all the dollar signs from the start of column 4 in my data.tsv file, then everything works.
My question: Is there a way to get gnuplot to accept or skip over the "$" in the prices in column #4?
(This is not an answer to your question but an answer to your problem so be welcome to accept another answer if somebody comes up with something better)
Just preprocess you data file before plotting and remove the $:
sed s/\$//g data.tsv >data2.tsv
I tried to do (more-or-less) what #kebs suggested, but it failed with the same "x range is invalid" error message when I was calling it like this within gnuplot:
plot "< sed s/\$//g data.tsv" using 1:4 title "Blah"
I'm not too familiar with sed, so I tried instead to use tr, and that worked like a charm.
Line 9 of my gnuplot script was this:
plot "data.tsv" using 1:4 title "Blah"
I changed it to say this:
plot "< cat data.tsv | tr --delete '$'" using 1:4 title "Blah"

How to fix gnuplot -e "plot 'file' u 1:($2)-1 w l" syntax?

I need to plot files using gnuplot without going to gnuplot terminal. So, I am taking a quick look at the plots using the following line.
i=2; while [ $i -le 14 ] ; do gnuplot -e "plot 'pop05' u 1:$i w l, 'pop01' u 1:$i w l; pause 2"; ((i++)); done
However, gnuplot -e does not seem to work for the cases of
gnuplot -e "plot 'pop01' u 1:($2)-1 w l"
ie, when I try to use an altered value in a particular column like I subtract 1 from the second column. However, plot 'file' u 1:($2)-1 w l works perfectly in gnuplot terminal. What should be the syntax for me to plot an altered column in the loop as well as without the loop?
I use gnuplot 4.4 patchlevel 3.
$2 has a special meaning in shell in double quotes (it returns the second positional argument). Just backslash the dollar sign:
gnuplot -e "plot 'pop01' u 1:(\$2)-1 w l"

Howto improve a coloured line plot by key in third column?

I'm trying to plot temperatures of my laptop, here are my working files at github
I have epoch, temperature & kernel identifier data like so:
1357786501 72 3.6.11-1-ARCH
1357786801 72 3.6.11-1-ARCH
1357787101 60 3.0.57-1-lts
1357787401 54 3.0.57-1-lts
1357800301 52 3.0.57-1-lts
1357800601 48 3.6.11-1-ARCH
1357800902 45 3.6.11-1-ARCH
The closest I've got to what I want is (using this):
set term svg size 1024,708
set xdata time
set key outside
set timefmt "%s"
set ytics 5
set format x "%d/%m"
plot "< awk '{if($3 == \"3.0.57-1-lts\") print}' temp.csv" u 1:2 t column(3) w p pt 2, \
"< awk '{if($3 == \"3.6.10-1-ARCH\") print}' temp.csv" u 1:2 t column(3) w p pt 2, \
"< awk '{if($3 == \"3.6.11-1-ARCH\") print}' temp.csv" u 1:2 t column(3) w p pt 2, \
"< awk '{if($3 == \"3.8.1-1-mainline-dirty\") print}' temp.csv" u 1:2 t column(3) w p pt 2
gihub kaihendry
Is there a way to avoid using awk? When I don't use this awk, it fails to plot differing kernel identifiers.
Can I make it a continuous line with different colours instead somehow?
Any ideas how to make SVG output without width/height? Any tricks to make it look better?
If your version of Gnuplot is recent enough, you can use a for-loop and iterator to loop over the different strings you want to match, see for example manual page 88-89 or this blog entry. To ignore non-matching lines you can use the ternary operator (cond ? iftrue : iffalse) to set these values to "Not-A-Number" (1/0 or NaN):
set xdata time
set key outside
set timefmt '%s'
set ytics 5
set format x '%d/%m'
set style data linespoints
archs = "`cut -d' ' -f3 temp.csv | sort -u | tr '\n' ' '`"
plot for [arch in archs] 'temp.csv' using 1:((strcol(3) eq arch) ? $2:1/0) title arch

Empty space in GNUPLOT graph

I am encountering strange behavior on my gnuplot script. The objective of this script is to read in a file and plot a specific set of lines (3 consecutive lines based on a given start point in the file) using the very first line of the file as the series headers.
While the plot works conceptually, I am encountering a large insert into the image on the left side, as if an empty line is read and plotted as 0 (with no header)
Input File:
Level,Filter,Type,Set1,Set2,Set3
Level1,Filter1,Type1,112,186,90
Level1,Filter1,Type2,233,335,159
Level1,Filter1,Type3,224,332,157
Code:
set terminal postscript color
set output '| epstopdf --filter --outfile=output.pdf'
set boxwidth 0.5
set style fill solid
set style data histograms
set datafile separator ","
LINE1 = 1 + 3 * COUNT
LINE2 = LINE1 + 1
LINE3 = LINE1 + 2
plot '../test.csv' \
u ( ( int($0) == LINE1 || int($0) == LINE2 || int($0) == LINE3)? $4 : 1/0) ti col,'' \
u ( ( int($0) == LINE1 || int($0) == LINE2 || int($0) == LINE3)? $5 : 1/0) ti col,'' \
u ( ( int($0) == LINE1 || int($0) == LINE2 || int($0) == LINE3)? $6 : 1/0) ti col
Command Line Call
>gnuplot -e "COUNT=0" test.plot
How can I get rid of the empty fields that lead to the right shift?
My gnuplot version is 4.6.
Since you're already using pipes and unix-ish tools, I would use sed here as well:
set term post color
set output 'foo.ps'
set style data histograms
set style histogram clustered
set datafile separator ","
set boxwidth 0.5
set style fill solid
SED_CMD = sprintf('< sed -n -e 1p -e %d,%dp test.csv',COUNT*3+2,COUNT*3+4)
plot for [COL=4:6] SED_CMD u COL ti col
I've simplified a lot of things while I was trying to figure out what your script was doing -- I used plot iteration (introduced in gnuplot 4.3). Originally I had thought that plot '...' every ... would work, but histograms seem to choke on every and I don't (yet!) understand why.
Here's an explanation of the sed command:
-e 1p #print first line in file
-e %d,%dp #print n'th line through m'th line (inclusive) where n=COUNT*3+2 and m=COUNT*3+4
If you're worried about shell injection, this seems to be safe as well:
gnuplot -e 'COUNT=";echo hi"' -persist test.gp
"test.gp", line 10: Non-numeric string found where a numeric expression was expected
Gnuplot will only write numbers to your command string.

Can the string for "set title" be obtained from a file?

I'm using Gnuplot with scripts and data files.
In my script there is a command;
set title "blah title here"
Is it possible to have that string taken from a data file? e.g. such that I can use a single script with many data files, because the data file will contain the title for the plot.
I'm not sure if this would be easy to do in pure gnuplot, but here is a solution using a wrapper bash script. You would use the script by calling plotscript.sh data.dat at the command line.
#!/bin/bash
my_title=$(head -n 1 $1 | sed 's/^# \(.*\)/\1/')
echo "set terminal postscript enhanced color
set output 'plot.eps'
set title '$my_title'
plot '$1' u 1:2" | gnuplot
To make the script usable put the code in a textfile and run chmod +x on it. If you tell me what format the title is in I can try to tailor the script to match that. This script assumes that the title is the first line of the data file in this type of format:
# mytitle
1 4
2 5
3 2
you can use backtic substitution...e.g.
set title "`head -1 datafile.dat`"
However, that doesn't quite get what you want since the backtic substitution is done prior to string operations (You can't specify the datafile name as a string). However, Macros are expanded prior to backtic substitutions.
My test datafile looked like:
"this is the title"
10 20
20 30
30 40
And my test script looked like:
DATAFILE="datafile.dat"
set macro
TI='`head -1 '.DATAFILE.'`' #macro: Single quotes are important here to prevent expansion of backtics.
set title #TI
plot DATAFILE u 1:2 title columnhead(1)
Note that if your title isn't enclosed in double quotes in the datafile, you'll need to add
them so that the resulting set title command is valid. (You can either add them to the macro, or to the datafile)
Even if this is rather late and the OP's account doesn't exist anymore, I need to add an answer, because it is simply not true that you cannot extract a title from a datafile with gnuplot only.
You can run stats (check help stats) without actually being interested in statistics but just for extracting the title.
You limit the data to the line of interest via every (check help every).
This works for gnuplot 4.6.0 (March 2012).
For gnuplot>=4.6.0 you can set a character as datafile separator (check help datafile separator). Take a character which doesn't appear in the line with the title. For gnuplot>=4.6.0 you can set datafile separator "\t" or for gnuplot>=5.0.0 you can also set datafile separator "\n".
Data: SO10968529.dat
# This is a commented line
"Line2: This is a uncommented line in double quotes"
"Line3: my Title"
Line5: This is a title without quotation marks
"Line6: Another title"
# x y
1 5.0
2 3.0
3 4.0
4 2.0
# end of data
Script:
### read title from datafile
reset
FILE = "SO10968529.dat"
set multiplot layout 1,2
set datafile separator "\t"
stats FILE u (myTitle=strcol(1),0) every ::0:0:0:0 nooutput
set datafile separator whitespace
set title myTitle
plot FILE u 1:2 w lp pt 7 lc rgb "red"
set datafile separator "\t"
stats FILE u (myTitle=strcol(1),0) every ::0:1:0:1 nooutput
set datafile separator whitespace
set title myTitle
plot FILE u 1:2 w lp pt 7 lc rgb "blue"
unset multiplot
### end of script
Result:

Resources