了解gnuplot如何使用awk脚本

irr*_*rom 6 awk gnuplot

我有一个数据文件

data.txt

1 1
2 2
3 3
5 4
7 5
Run Code Online (Sandbox Code Playgroud)

我试图了解gnuplot如何使用awk.我可以用它来绘制它plot "<awk '{print $1,$2}' data.txt".但是,当我尝试load '<./script.sh data.txt'它不起作用.

script.sh

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

' $1
Run Code Online (Sandbox Code Playgroud)

使用该script.sh方法返回错误:

gnuplot> plot 1 1

              ^
Run Code Online (Sandbox Code Playgroud)

"<./ script.sh data.txt",第1行:意外或无法识别的令牌

在我看来,我的awk脚本是内联awk语句的功能等价物.为什么这个script.sh方法不起作用?

仅供参考,我知道我可以做plot "data.txt" u 1:2我的数据.这只是我想要解决的更复杂问题的理想化版本.

Dil*_*ant 4

这应该与 awk 无关,但与 gnuplots 对绘图和加载命令的期望有关。

根据我的理解,阅读 gnuplot 帮助输出并尝试您提供的示例:

plot "<awk '{print $1,$2}' data.txt"
Run Code Online (Sandbox Code Playgroud)

只是一种复杂的方法,通过系统上可用的 popen 调用提供类似对象的文件,然后绘图命令从 x、y 点读取。

您的第二个脚本做了一些不同的事情,因为加载命令现在在第一行中接收到它无法满足的命令(即绘图后跟 x 和 y 值),并且即使没有任何前缀命令(下一行简单地2 2在这个案例。

在我的记忆中,gnuplot 的活跃使用是几年前(如果不是几十年的话);-) - load 就像 well load,您可以在其中从模块编写绘图代码,但这些必须包含有效的 gnuplot 命令。

我的系统上的负载帮助给出:

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.
Run Code Online (Sandbox Code Playgroud)

我总是解决生成匹配文件的问题,并且对 gnuplot 调用有一些调用魔法来参数化绘图。