2012-10-11 2 views
16

Ich will das so etwas wie:mit awk oder anderem Shell-Befehl in gnuplot Funktion

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 

Was nicht funktioniert, ist der Teil mit awk innerhalb einer gnuplot-Funktion. meinen awk Skript direkt Verwendung nach plot funktioniert:

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 

Gibt es eine Möglichkeit awk oder andere Shell-Befehle in einer gnuplot Funktion zu setzen? Ich weiß, ich könnte das awk-Skript in eine andere Datei auslagern und diese Datei direkt nach plot awk, aber ich möchte keine separaten Dateien.

Antwort

23

$var nicht erweitert var in einer Zeichenfolge innerhalb gnuplot wie es in einer Shell wäre. Sie wollen gnuplot die String-Verkettung (die verwendet den . Operator) verwenden:

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

Oder ich nehme an, Sie sprintf verwenden könnte, wenn Sie, dass bequemer finden:

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

* Beachten Sie, dass diese doesn‘ t den Befehl tatsächlich ausführen. Es wird nur die Zeichenfolge erstellt, die an plot übergeben wird und dann plot den Befehl ausführt. Wenn Sie einen Befehl in einer Funktion tatsächlich ausführen möchten, können Sie system als Funktion aufrufen. Aus der Dokumentation:

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

Perfekt! Ich habe jetzt auch eine weitere Hilfsfunktion hinzugefügt 'exec (cmd, arg1) = (System (sprintf (cmd, arg1)))' die entbindet mich von 'System (sprintf())' mehrmals eingeben ' – Juve

+0

Ich sah gerade, dass meine 'exec' funktioniert nicht mit' plot'. Wie Sie sagten, erwartet "plot" eine Zeichenkette, wie sie von Ihrem zweiten Snippet erzeugt wird. Danke, dass du das herausgebracht hast. – Juve