抱歉,标题太混乱了。我找不到更好的题目来描述这个问题。我正在使用gnuplot来绘制一些文件。我试图绘制的文件位于一个名为“进程”的文件夹中。在process文件夹内是另一个名为100的文件夹,结构如下;
“绘图”是我记得使用gnuplot的文件。
所述文件图具有以下行;
file='file.dat'
time = "cat file | grep 'time' | cut -d' ' -f2 | tr -d ';' | awk 'NR==1{print $1}'"
plot \
      "process/".time."/speed.txt" using 1:3 with line lt 2 lc 6 title ""因此,我们的目标是在file.dat中查找单词time并削减它的值(在本例中为100 ),并将其用作文件夹名,在这里我试图绘制speed.txt文件。然而,当我执行gnuplot时,我所拥有的似乎不起作用。有人能帮忙吗?
非常感谢!
发布于 2020-10-07 23:25:50
首先,要从gnu图运行shell命令并将其标准输出作为文本,请使用system函数。
filter_command = "cat ..." 
time = system( filter_command )其次,命令字符串"cat .“如果您只是将其传递给system,则您定义的它将不能正常工作。您打算将cat file中的字符串cat file.dat从gnuplot的file变量中展开为cat file.dat。要做到这一点,我们需要再走一步。有两种方法可以做到这一点。
使用运算符的. Concatnating字符串
file='file.dat'
filter_command = "cat " . file . " | grep 'time' | cut -d' ' -f2 | tr -d ';' | awk 'NR==1{print $1}'" sprintf函数file='file.dat'
filter_template = "cat %s | grep 'time' | cut -d' ' -f2 | tr -d ';' | awk 'NR==1{print $1}'" 
filter_command  = sprintf(filter_template, file)脚本
最后的剧本是这样的。
file='file.dat'
filter_template = "cat %s | grep 'time' | cut -d' ' -f2 | tr -d ';' | awk 'NR==1{print $1}'" 
filter_command  = sprintf(filter_template, file)
time = system( filter_command )
plot "process/".time."/speed.txt" using 1:3 with line lt 2 lc 6 title ""发布于 2020-10-07 19:42:57
虽然gnu图不是为解析文件而创建的,但是您还是可以这样做,有时使用奇怪的变通方法。当然有一种方法可以用您提到的工具来实现: cat、grep、cut、tr和awk。然而,并不是每个人都在使用Linux,而且手头有这些工具。因此,如果可能的话--尽管它不是最优的--我个人更倾向于拥有独立于平台的“gnuplot”解决方案。
因此,下面的代码基本上是逐行将文件file.dat“绘制”成一个虚拟表,每次检查当前行是否包含字符串time:。如果是,将行的其余部分写入变量myValue。
获取有关命令的更多信息:help strstrt、help strlen、help strcol、help ternary、help datafile separator。
文件: file.dat
### file.dat
This is a data file
which contains something
but also a line with
time:100
And many more things...
Maybe also some data...?
1  1.1
2  2.2
3  3.3
4  4.4
# end of file代码:
### extract key&value from a file and use it in path
reset session
myFile = 'file.dat'
myKey = 'time:'
myValue = ''
myPath(s) = sprintf('process/%s/speed.txt',s)
getValue(line) = strstrt(line,myKey) > 0 ? myValue = line[strstrt(line,myKey)+strlen(myKey):] : myValue
# extract the value for key
set datafile separator "\n"
set table $Dummy
    plot myFile u (getValue(strcol(1))) w table
unset table
set datafile separator whitespace
print myValue
print myPath(myValue)
# Your plot command would then look, e.g. like this:
plot myPath(myValue) using 1:3 with line lt 2 lc 6 title ""
### end of code结果:
100
process/100/speed.txthttps://stackoverflow.com/questions/64236470
复制相似问题