了解gnuplot如何使用awk脚本

时间:2016-07-08 12:23:22

标签: awk gnuplot

我有一个数据文件

data.txt

1 1
2 2
3 3
5 4
7 5

我试图了解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

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

  

的gnuplot&GT;情节1 1

              ^
     

“&lt; ./ script.sh data.txt”,第1行:意外或无法识别的令牌

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

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

2 个答案:

答案 0 :(得分:2)

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

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

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

只是一种复杂的方式,通过popen调用在系统上提供像object这样的文件,plot命令然后从x,y点读取。

你的第二个脚本做了一些不同的事情,因为load命令现在在第一行接收到它不能满足的命令(即绘图后跟x和y值),即使没有任何前缀命令也会接收后续命令(下一个)在这种情况下,只需2 2行。

在我对活动gnuplot使用的记忆中 - 几年前,如果不是几十年;-) - 加载就像加载一样,你可以从模块中编写绘图代码,但这些必须包含有效的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.

我总是解决生成匹配文件的问题,并为gnuplot调用提供一些调用魔法来参数化这些图。

答案 1 :(得分:0)

您的脚本等同于awk的内联调用,这是将数据流式传输到plot命令的一种方法。 plot-command文件中的流数据是使用'-'完成的,这就是你需要使用的。

将您的awk脚本更改为:

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

请注意单引号的奇怪转义方式,请参阅how to escape single quote in awk inside printf

相关问题