6

I have a data file

data.txt

1 1
2 2
3 3
5 4
7 5

and I am trying to understand how to gnuplot uses awk. I can plot it with plot "<awk '{print $1,$2}' data.txt". However, when I try load '<./script.sh data.txt' it does not work.

script.sh

#!/bin/bash
awk 'BEGIN {
         printf "plot ";
    }
    {
        printf "%i %i\n",$1,$2
    }
    
' $1

Using the script.sh method returns the error :

gnuplot> plot 1 1

              ^

"<./script.sh data.txt", line 1: unexpected or unrecognized token

It seems to me that my awk script is the functional equivalent of the inline awk statement. Why doesn't the script.sh method work?

FYI, I am aware that I can just do plot "data.txt" u 1:2 to plot my data. This is just an idealized version of a more complex problem that I am trying to solve.

Community
  • 1
  • 1
irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60

2 Answers2

4

This should have nothing to do with awk, but all with gnuplots expectation for plot and load commands.

In my understanding reading gnuplot help output and trying the samples you provided:

plot "<awk '{print $1,$2}' data.txt"

is just a complicated way, to offer via popen call available on your system a file like object, the plot command then reads from the x, y points.

You second script does something different, as the load command now receives in a first line a command it cannot satisfy (namely plot followed by an x and an y value) and subsequent commands would be received even without any prefix command (next line simply 2 2 in this case.

In my memory of active gnuplot usage - years ago if not decades ;-) - load is like well load, where you can compose plotting code from modules, but these must contain valid gnuplot commands.

The help for load on my system gives:

gnuplot> help load
 The `load` command executes each line of the specified input file as if it
 had been typed in interactively.  Files created by the `save` command can
 later be `load`ed.  Any text file containing valid commands can be created
 and then executed by the `load` command.  Files being `load`ed may themselves
 contain `load` or `call` commands.  See `comments` for information about
 comments in commands.  To `load` with arguments, see `call`.

 Syntax:
       load "<input-file>"

 The name of the input file must be enclosed in quotes.

 The special filename "-" may be used to `load` commands from standard input.
 This allows a `gnuplot` command file to accept some commands from standard
 input.  Please see help for `batch/interactive` for more details.

 On some systems which support a popen function (Unix), the load file can be
 read from a pipe by starting the file name with a '<'.

 Examples:
       load 'work.gnu'
       load "func.dat"
       load "< loadfile_generator.sh"

 The `load` command is performed implicitly on any file names given as
 arguments to `gnuplot`.  These are loaded in the order specified, and
 then `gnuplot` exits.

I always solved generating matching files and have some call magic for the gnuplot calls to parametrize the plots.

Dilettant
  • 3,267
  • 3
  • 29
  • 29
  • 1
    How is this reconciled with an example like this : http://gnuplot-tricks.blogspot.com/2009/06/3d-bargraphs-in-gnuplot-one-more-time.html where they use awk to print "splot" in awk and then print out the appropriate arrays and gnuplot magically handles it? – irritable_phd_syndrome Jul 12 '16 at 11:26
  • `load "< loadfile_generator.sh"` is also listed as example in the quote given in the answer. An in the link a bunch of parametric functions are written, but no inline data. – Christoph Jul 12 '16 at 16:43
0

Your script is not equivalent to the inline invocation of awk, which is one way to stream data to the plot command. Streaming data from within the plot-command file is done with a '-', that's what you need to use.

Change your awk script to:

#!/bin/bash
awk 'BEGIN {
         printf "plot '\''-'\''\n";
    }
    {
        printf "%i %i\n",$1,$2
    }
END {
        printf "e"
    }
' $1

Note the weird escaping way for the single quote, see how to escape single quote in awk inside printf

Community
  • 1
  • 1
Joce
  • 2,220
  • 15
  • 26