bash GNUplot 输入文件名作为变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25341608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:09:32  来源:igfitidea点击:

GNUplot input filename as variable

bashvariablesgnuplot

提问by Shenan

I want to input the filename to GNUplot as a variable. I've written a gnuplot script, which includes the following lines:

我想将文件名作为变量输入到 GNUplot。我编写了一个 gnuplot 脚本,其中包括以下几行:

path="path/to/file"
plot "< cat $path | sed '1,4d'" using 1:2

When I run the script, the gnuplot window opens but there's nothing in it. If I replace $pathwith the actual path, the graph is correctly plotted.

当我运行脚本时,gnuplot 窗口会打开,但里面什么也没有。如果我替换$path为实际路径,则图形会正确绘制。

Please can you suggest a way of doing this? Thank you.

请问你能建议一种方法吗?谢谢你。

回答by Tom Fenech

You can do this to interpolate the value of the gnuplot variable in your command:

您可以这样做以在命令中插入 gnuplot 变量的值:

path="path/to/file"
plot "< cat ". path ." | sed '1,4d'" using 1:2

Note that it is not necessary to use catand sedtogether:

注意,没有必要同时使用catsed

path="path/to/file"
plot "<sed '1,4d' ". path using 1:2

Be careful with your spaces! You can use printinstead of plotto see what command will be executed.

小心你的空间!您可以使用print而不是plot查看将执行什么命令。

Sometimes, I like to use sprintfto do this kind of thing:

有时候,我喜欢用来sprintf做这种事情:

cmd = "1,4d"
plot sprintf("<sed '%s' %s", cmd, path) using 1:2

It's a bit longer but it's also clearer in my opinion.

它有点长,但在我看来也更清晰。

By the way, in this particular case, you don't need to use any external tools at all. You could just do:

顺便说一下,在这种特殊情况下,您根本不需要使用任何外部工具。你可以这样做:

plot path using 1:2 every ::4

Which will skip the first 4 lines of your file.

这将跳过文件的前 4 行。