Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding how gnuplot uses awk scripts

Tags:

awk

gnuplot

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.

like image 913
irritable_phd_syndrome Avatar asked Jul 08 '16 12:07

irritable_phd_syndrome


People also ask

What language does gnuplot use?

The supported operators in Gnuplot are the same as the corresponding operators in the C programming language, except that most operators accept integer, real, and complex arguments. The ** operator (exponentiation) is supported as in FORTRAN. Parentheses may be used to change the order of evaluation.

What is gnuplot used for?

gnuplot is a command-line and GUI program that can generate two- and three-dimensional plots of functions, data, and data fits. The program runs on all major computers and operating systems (Linux, Unix, Microsoft Windows, macOS, FreeDOS, and many others).

How can I call gnuplot?

You can run a script two ways: Type load "scriptname" from within gnuplot. Or, from UNIX, run gnuplot by typing gnuplot scriptname . In this method, gnuplot will exit when your script is finished, so you may want to include PAUSE -1 "Hit any key to continue" as your last line.


1 Answers

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.

like image 146
Dilettant Avatar answered Oct 17 '22 04:10

Dilettant