首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在gnuplot函数中使用awk或其他shell命令

在gnuplot函数中使用awk或其他shell命令
EN

Stack Overflow用户
提问于 2012-10-12 03:03:04
回答 1查看 21.2K关注 0票数 17

我想要这样的东西:

代码语言:javascript
复制
file1='logs/last/mydata1.log'
file2='logs/last/mydata2.log'

# declare function that uses awk to reshape the data - does not work :(
sum1(fname)=("<awk '{sum=0; for(i=8;i<=NF;i+=2) sum+=$i; print $1,sum/2}' $fname")
sum2(fname)=("<awk '{sum=0; for(i=9;i<=NF;i+=2) sum+=$i; print $1,sum/2}' $fname")

# plot different columns of my file and awk processed file
plot file1 u 1:2 title "thing A measure 1" w l, \
     file1 u 3:4 title "thing A measure 2" w l, \
     file2 u 1:2 title "thing B measure 1" w l, \
     file2 u 3:4 title "thing B measure 2" w l, \
     sum1(file1) u 1:2 title "group A measure 1" w l, \
     sum2(file1) u 1:2 title "group A measure 2" w l, \
     sum1(file2) u 1:2 title "group B measure 1" w l, \
     sum2(file2) u 1:2 title "group B measure 2" w l

不起作用的是gnuplot函数中包含awk的部分。在plot之后直接使用我的awk脚本可以很好地工作:

代码语言:javascript
复制
plot "<awk '{sum=0; for(i=9;i<=NF;i+=2) sum+=$i; print $1,sum}' ./logs/last/mydata1.log" u 1:2 w l

有没有办法将awk或其他shell命令放入gnuplot函数中?我知道我可以将awk脚本外包给另一个文件,然后在plot之后直接awk这个文件,但我不想要单独的文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-12 20:05:58

$var不会像在shell中那样在gnuplot中的字符串中展开var。您希望使用gnuplot的字符串连接(它使用.操作符):

代码语言:javascript
复制
sum1(fname)="<awk '{sum=0; for(i=8;i<=NF;i+=2) sum+=$i; print $1,sum/2}' ".fname

或者,如果你觉得sprintf更方便的话,我想你可以使用它:

代码语言:javascript
复制
sum1(fname)=sprintf("<awk '{sum=0; for(i=8;i<=NF;i+=2) sum+=$i; print $1,sum/2}' %s",fname)

*请注意,这并不实际执行命令。它只是构建字符串,该字符串将被传递给plot,然后plot将执行该命令。如果您实际上想要在函数中执行命令,则可以将system作为函数调用。从文档中:

代码语言:javascript
复制
`system "command"` executes "command" using the standard shell. See `shell`.
 If called as a function, `system("command")` returns the resulting character
 stream from stdout as a string.  One optional trailing newline is ignored.

 This can be used to import external functions into gnuplot scripts:

       f(x) = real(system(sprintf("somecommand %f", x)))
票数 24
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12846717

复制
相关文章

相似问题

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