首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >grep文件中的值,然后使用它作为文件夹名来定位文件并绘制

grep文件中的值,然后使用它作为文件夹名来定位文件并绘制
EN

Stack Overflow用户
提问于 2020-10-07 02:46:28
回答 2查看 245关注 0票数 1

抱歉,标题太混乱了。我找不到更好的题目来描述这个问题。我正在使用gnuplot来绘制一些文件。我试图绘制的文件位于一个名为“进程”的文件夹中。在process文件夹内是另一个名为100的文件夹,结构如下;

“绘图”是我记得使用gnuplot的文件。

所述文件图具有以下行;

代码语言:javascript
运行
复制
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时,我所拥有的似乎不起作用。有人能帮忙吗?

非常感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-07 23:25:50

首先,要从gnu图运行shell命令并将其标准输出作为文本,请使用system函数。

代码语言:javascript
运行
复制
filter_command = "cat ..." 
time = system( filter_command )

其次,命令字符串"cat .“如果您只是将其传递给system,则您定义的它将不能正常工作。您打算将cat file中的字符串cat file.dat从gnuplot的file变量中展开为cat file.dat。要做到这一点,我们需要再走一步。有两种方法可以做到这一点。

使用运算符的. Concatnating字符串

代码语言:javascript
运行
复制
file='file.dat'
filter_command = "cat " . file . " | grep 'time' | cut -d' ' -f2 | tr -d ';' | awk 'NR==1{print $1}'" 

  1. 使用sprintf函数

代码语言:javascript
运行
复制
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)

脚本

最后的剧本是这样的。

代码语言:javascript
运行
复制
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 ""
票数 1
EN

Stack Overflow用户

发布于 2020-10-07 19:42:57

虽然gnu图不是为解析文件而创建的,但是您还是可以这样做,有时使用奇怪的变通方法。当然有一种方法可以用您提到的工具来实现: cat、grep、cut、tr和awk。然而,并不是每个人都在使用Linux,而且手头有这些工具。因此,如果可能的话--尽管它不是最优的--我个人更倾向于拥有独立于平台的“gnuplot”解决方案。

因此,下面的代码基本上是逐行将文件file.dat“绘制”成一个虚拟表,每次检查当前行是否包含字符串time:。如果是,将行的其余部分写入变量myValue

获取有关命令的更多信息:help strstrthelp strlenhelp strcolhelp ternaryhelp datafile separator

文件: file.dat

代码语言:javascript
运行
复制
### 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

代码:

代码语言:javascript
运行
复制
### 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

结果:

代码语言:javascript
运行
复制
100
process/100/speed.txt
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64236470

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档