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

Juv*_*uve 16 awk 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
Run Code Online (Sandbox Code Playgroud)

什么是无效的是awk内部gnuplot功能的部分.工作正常awk后直接使用我的脚本plot:

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
Run Code Online (Sandbox Code Playgroud)

有没有办法awk在gnuplot函数中放置或其他shell命令?我知道我可以将awk脚本外包到另一个文件并直接唤醒​​此文件plot,但我不想要单独的文件.

mgi*_*son 23

$var不会var像在shell中那样在gnuplot中的字符串中展开.您想使用gnuplot的字符串连接(使用.运算符):

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

或者,我想你可以使用,sprintf如果你觉得更舒服:

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

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

`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)))
Run Code Online (Sandbox Code Playgroud)