I\'m trying to produce a plot with two lines using data taken from stdin. I have a file \"test.csv\":
0,1.1,2
1,2,3
2,6,4
4,4.6,5
5,5,6
I\
Gnuplot can read from stdin, but for each plot statement, a new data set is required. The following works fine:
cat test.csv | gnuplot -p -e "set datafile separator ','; plot '-' using 1:2 w l"
The error appears as soon as you append the second plot command with , '' using 1:3
. For this you need to send the data again as the first data set isn't stored interally for reuse. So for you two plot commands, the following snippet works fine:
echo 'e' | cat test.csv - test.csv | gnuplot -p -e "set datafile separator ','; plot '-' using 1:2 w l, '' using 1:3 w l"
That writes the data file twice, separated by an e
which indicates the end of the data for the first plot command.
gnuplot seems to need random access (i.e. not stdin), so I think you're stuck with
# explicitly open "test" file
$ gnuplot -p -e "set datafile separator \",\"; plot 'test' using 1:2 with lines, '' using 1:3 with lines;"
The "-" is used to specify that the data follows the plot command. So if you use it, you'll need to do something like:
echo "set datafile separator \",\"; plot '-' using 1:2 with lines, '' using 1:3 with lines;" | cat - datafile.dat | gnuplot -p
(Quoting above probably needs to be escaped).
What're you looking for is this:
plot '< cat -'
Now, you can do:
cat test | sed ... | gnuplot -p "plot '< cat -' using ..."
Note that you might need to feed in the input data via stdin multiple times if you're using options with plot, like so:
cat testfile testfile | gnuplot -p "plot '< cat -' using 1, '' using 2"
In the above case, testfile must end with a line that has the sole character 'e' in it.
Manual reference
Have you tried chart? Your case would be as easy as:
cat test | chart line ','
And would give you:
I'd try converting the csv file to space separated (assuming no of the records span multiple lines) by piping it through sed
instead of setting the separator:
cat test | sed 's/,/ /g' | gnuplot -p -e "plot '-' using ..."
I had to do the following, since adding '-' did not work for me:
cat filename | awk -- '{print $0} END{print "e"}' | tee -i -a /dev/stdout /dev/stdout