用awk或其他shell命令的gnuplot函数内 [英] using awk or other shell command inside gnuplot function

查看:171
本文介绍了用awk或其他shell命令的gnuplot函数内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想是这样的:

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 部分。
后直接剧情使用我的 AWK 脚本正常工作:

What is not working is the part with awk inside a gnuplot function. Using my awk script directly after plot works fine:

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

有没有把一个gnuplot的函数中 AWK 或其它shell命令的方法吗?
我知道我可以外包awk脚本到另一个文件和剧情后直接在awk这个文件,但我不想单独的文件。

Is there a way to put awk or other shell commands inside a gnuplot function? I know I could outsource the awk script to another file and awk this file directly after plot, but I don't want separate files.

推荐答案

$ VAR 不展开 VAR 在gnuplot的中的字符串像它会在一个外壳。您要使用的gnuplot的字符串连接(使用 运算符):

$var doesn't expand var in a string within gnuplot like it would in a shell. You want to use gnuplot's string concatenation (which uses the . operator):

sum1(fname)="<awk '{sum=0; for(i=8;i<=NF;i+=2) sum+=$i; print $1,sum/2}' ".fname

或者,我想你可以使用的sprintf 如果你发现更舒适:

sum1(fname)=sprintf("<awk '{sum=0; for(i=8;i<=NF;i+=2) sum+=$i; print $1,sum/2}' %s",fname)

*请注意,这实际上并不执行命令。它只是建立将被传递给剧情然后剧情将执行该命令字符串。如果你确实想在一个函数来执行命令,你可以叫系统作为函数。从文档:

*Note that this doesn't actually execute the command. It just builds the string which will be passed to plot and then plot will execute the command. If you actually want to execute a command in a function, you can call system as a function. From the docs:

`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)))

这篇关于用awk或其他shell命令的gnuplot函数内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆