简体   繁体   English

如何在 shell 命令行中使用带有 awk 的 gnuplot (-e swtich)

[英]How can I use gnuplot with awk in shell command line (-e swtich)

I'd like to use gnuplot with awk in shell command line like below.我想在 shell 命令行中使用带有awk gnuplot ,如下所示。

gnuplot -persist -e " plot '< awk_command' "

My awk command is我的awk命令是

awk '/match_pattern/ {print $4}' log.txt

I cannot find how to handle quotes inside the my awk command.我在我的 awk 命令中找不到如何处理引号。

gnuplot -persist -e "plot " < awk '/matchpattern/ {print $4}' log.txt " "

This produced an error message that这产生了一条错误消息

awk: No such file or directory.

When I did this in gnuplot , there was no problem.当我在gnuplot这样做时,没有问题。

gnuplot> plot " < awk '/matchpattern/ {print $4}' log.txt "

I want to plot it in shell command line not in gnuplot just for convenience.为了方便起见,我想在 shell 命令行中而不是在 gnuplot 中绘制它。

From the Gnuplot documentation:从 Gnuplot 文档:

Command-line substitution is specified by a system command enclosed in backquotes.命令行替换由用反引号括起来的系统命令指定。 This command is spawned and the output it produces replaces the backquoted text on the command line生成此命令,它产生的输出替换命令行上的反引号文本

source: GnuPlot 5.0 documentation来源: GnuPlot 5.0 文档

So this can easily be tested in gnuplot itself:所以这可以很容易地在 gnuplot 本身中进行测试:

gnuplot> `awk 'BEGIN{print "plot sin(x)"}'`

If you now want to use this with the -e flag from the command-line, it should look something like this:如果您现在想在命令行中将它与-e标志一起使用,它应该如下所示:

$ gnuplot --persist -e "`awk 'BEGIN{print \"plot sin(x)\"}' ` "

Notice that we had to escape the <double-quotes> inside the awk command.请注意,我们必须对 awk 命令中的 <double-quotes> 进行转义。 This is due to bash quoting rules.这是由于 bash 引用规则。 If you do not want to escape the double quotes, you will have to use single quotes and escape these again.如果您不想转义双引号,则必须使用单引号并再次转义它们。 That will than look like this:这将看起来像这样:

$ gnuplot --persist -e '`awk '"'"'BEGIN{print "plot sin(x)"}'"'"' ` '

If you use awk just to generate the data and you want to use the pipe system of gnuplot:如果你只是使用awk来生成数据并且你想使用 gnuplot 的管道系统:

On systems with a popen function, the datafile can be piped through a shell command by starting the file name with a '<'.在具有 popen 函数的系统上,数据文件可以通过 shell 命令通过以“<”开头的文件名进行管道传输。 For example,例如,

 pop(x) = 103*exp(-x/10) plot "< awk '{print $1-1965, $2}' population.dat", pop(x)

source: GnuPlot 5.0 documentation来源: GnuPlot 5.0 文档

So in case of the OP, with the proper escaping, you would do:因此,在 OP 的情况下,通过正确的转义,您可以执行以下操作:

$ gnuplot --persist -e "plot \"< awk '/matchpattern/{print \$4}' log.txt \""

notice how I also had to escape the $ , otherwise, bash would have substituted it with the 4th bash argument which is most likely an empty string.请注意我还必须如何转义$ ,否则,bash 会将其替换为第 4 个 bash 参数,这很可能是一个空字符串。

You just need to escape the nested double quotes:您只需要转义嵌套的双引号:

gnuplot -persist -e "plot \" < awk '/matchpattern/ {print $4}' log.txt \" "

If you don't do this your shell will interpret as separate arguments for gnuplot如果你不这样做,你的 shell 将解释为 gnuplot 的单独参数

  • -e "plot "
  • < awk
  • '/matchpattern/ {print $4}'
  • log.txt

and the shell will wrongly think that it should redirect the standard input of your gnuplot command from a file called awk .并且 shell 会错误地认为它应该从名为awk的文件重定向gnuplot命令的标准输入。 As the file does not exist in your working directory it throws the error: awk: No such file or directory.由于该文件在您的工作目录中不存在,它会引发错误: awk: No such file or directory.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM