17

私はこのようなものが欲しい:

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

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

awkgnuplot 関数内に他のシェル コマンドを配置する方法はありますか? awk スクリプトを別のファイルに外部委託し、このファイルを の直後に awk できることはわかっていますplotが、個別のファイルは必要ありません。

4

1 に答える 1

24

$varvarシェルのように gnuplot 内の文字列に展開されません。.gnuplot の文字列連結 (演算子を使用) を使用したい場合:

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)

*これは実際にコマンドを実行するわけではないことに注意してください。渡される文字列を作成してからplotplotコマンドを実行します。実際に関数内でコマンドを実行したい場合は、関数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)))
于 2012-10-12T12:05:58.217 に答える