【问题标题】:Understanding how gnuplot uses awk scripts了解 gnuplot 如何使用 awk 脚本
【发布时间】:2016-11-11 00:25:00
【问题描述】:

我有一个数据文件

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> 情节 1 1

              ^

“<. data.txt>

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

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

【问题讨论】:

    标签: awk gnuplot


    【解决方案1】:

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

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

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

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

    你的第二个脚本做了一些不同的事情,因为加载命令现在在第一行接收一个它不能满足的命令(即 plot 后跟一个 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 调用有一些调用魔法来参数化绘图。

    【讨论】:

    • 这如何与这样的示例相协调:gnuplot-tricks.blogspot.com/2009/06/… 他们使用 awk 在 awk 中打印“splot”,然后打印出适当的数组,gnuplot 神奇地处理它?
    • load "&lt; loadfile_generator.sh" 也作为示例在答案中给出的引用中列出。一个在链接里写了一堆参数函数,但是没有内联数据。
    【解决方案2】:

    您的脚本等同于awk 的内联调用,这是将数据流式传输到绘图命令的一种方式。来自 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      相关资源
      最近更新 更多