bash 使用 gnuplot 编写脚本——在 sed 处

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12223772/
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 03:09:37  来源:igfitidea点击:

Scripting with gnuplot--where sed

bashscriptingsedgnuplot

提问by Samuel Markson

So I'm looking for some quick-and-dirty solution.

所以我正在寻找一些快速而肮脏的解决方案。

The problem:

问题:

I am trying to plot a specific section of a data file with gnuplot. This is fine. The basic line goes something like

我正在尝试使用 gnuplot 绘制数据文件的特定部分。这可以。基本路线类似于

plot "<(sed -n '1,100p' pointsandstuff.dat)" u 1:log(**2+**2) notitle

This works just fine. The next step I want is to include in my title another part of the data, namely the data entry $3 (which for the points listed is identical, so I can parse it from anywhere). I run into problem because, while plot seems fine, I can't seem to feed regex info into 'title'. An example of something that doesn'twork"

这工作得很好。我想要的下一步是在我的标题中包含另一部分数据,即数据条目 $3(对于列出的点来说是相同的,所以我可以从任何地方解析它)。我遇到了问题,因为虽然情节看起来不错,但我似乎无法将正则表达式信息输入到“标题”中。一个不起作用的例子”

plot "<(sed -n '1,100p' pointsandstuff.dat)" u 1:log(**2+**2) title "<(sed -n '1,1p' pointsandstuff.dat)"

(This would spit out a whole data line, in theory, though in practice I just get the title "<(sed...")

(理论上,这会吐出一整条数据线,但实际上我只会得到标题“<(sed ...”)

I tried attacking this with a bash script, but the '$'s that I use throw the bash script into a tizzy:

我尝试用 bash 脚本攻击它,但是我使用的“$”使 bash 脚本陷入困境:

#!/bin/bash


STRING=$(echo|sed -n '25001,25001p' pointsandstuff.dat)
echo $STRING


 gnuplot -persist << EOF
 set xrange[:] noreverse nowriteback
 set yrange[:] noreverse nowriteback

 eval "plot "<(sed -n '25001,30000p' pointsandstuff.dat)" u 1:log(**2+**2) title $STRING


EOF

Bash won't know what to do with '$4' and '$5'.

Bash 不知道如何处理“$4”和“$5”。

采纳答案by Samuel Markson

Aha, thanks all. Have come up with a few solutions by now--the simplest being just escaping those $s from before (which I mistakenly thought gnuplot disliked...). To whit:

啊哈,谢谢大家。现在已经想出了一些解决方案——最简单的就是从之前转义那些 $s(我错误地认为 gnuplot 不喜欢......)。至于:

STRING=$(echo|sed -n '1,1p' spointsandstuff.dat)
echo $STRING


 gnuplot -persist << EOF
 set xrange[:] noreverse nowriteback
 set yrange[:] noreverse nowriteback

 eval "plot "<(sed -n '1,100p' pointsandstuff.dat)" u 1:(log($4**2+$5**2)) title '$STRING'
 !gv diag_spec.eps &

EOF

Thanks all, though--it's been a good excuse to play with this stuff...here's hoping that, if any poor soul sees this script later, it might be a bit easier on them.

不过,谢谢大家——这是玩这些东西的一个很好的借口……这里希望,如果以后有任何可怜的人看到这个脚本,对他们来说可能会容易一些。

回答by Jonathan Leffler

You seem to be attempting process substitution, but the double quotes stop it working in the first case and you need a command substitution in the second case.

您似乎正在尝试进程替换,但双引号使其在第一种情况下停止工作,而在第二种情况下您需要命令替换。

You have:

你有:

plot "<(sed -n '1,100p' pointsandstuff.dat)" u 1:log(**2+**2) \
      title "<(sed -n '1,1p' pointsandstuff.dat)"

You need:

你需要:

plot <(sed -n '1,100p' pointsandstuff.dat) u 1:log(**2+**2) \
      title "$(sed -n '1,1p' pointsandstuff.dat)"

The double quotes in the second case might not be strictly necessary, but you won't go wrong with them present.

第二种情况下的双引号可能不是绝对必要的,但你不会出错。

  • Process substitution generates a file name and feeds the output of the nested command into that file; the command thinks it is reading a file (because it is reading a file).

  • Command substitution captures the output of the nested command in a string and passes that string to the command (when it is used as an argument to a command, as here).

  • 进程替换生成一个文件名,并将嵌套命令的输出提供给该文件;该命令认为它正在读取一个文件(因为它正在读取一个文件)。

  • 命令替换捕获字符串中嵌套命令的输出并将该字符串传递给命令(当它用作命令的参数时,如下所示)。

回答by mgilson

My understanding of the question is a little hazy, but it looks like you want to plot the first 100 lines -- This is quite easy to do:

我对这个问题的理解有点模糊,但看起来你想绘制前 100 行——这很容易做到:

plot '< head -100 datafile.dat' u  ....

Of course, you can use sedif you wish (or awkor ...). A gnuplot only solution might look like this:

当然,sed如果您愿意(或awk或...),您可以使用。仅 gnuplot 的解决方案可能如下所示:

plot 'datafile.dat' u (
plot 'datafile.dat' every ::25001::30000 u 1:(log(**2+**2)
> 100? 1/0:):(log(**2+**2))

Or like this (which is more simple for regular selections):

或者像这样(对于常规选择更简单):

plot ... title "`head -1 datafile.dat | awk '{print }'`"

and explained in more detail in another answer.

并在另一个答案中更详细地解释。

Now, if you want the title to come from the datafile, you can parse it out using gnuplot's backtic substitution:

现在,如果您希望标题来自数据文件,您可以使用 gnuplot 解析它backtic substitution

plot ... title system("head -1 datafile.dat | awk '{print }'")

which is essentially the same as gnuplot's system command:

本质上与 gnuplot 的系统命令相同:

plot ... title columnhead(3)

but in this case, you mightbe able to use the columnheadfunction:

但在这种情况下,您可能可以使用该columnhead功能:

##代码##