2012-10-11 4 views
16

voglio qualcosa di simile:utilizzando awk o altro comando di shell all'interno funzione 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 

ciò che non funziona è la parte con awk all'interno di una funzione gnuplot. Usando il mio script awk direttamente dopo plot funziona bene:

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 

C'è un modo per mettere awk o altri comandi di shell all'interno di una funzione gnuplot? So che potrei esternalizzare lo script awk in un altro file e avviare questo file direttamente dopo plot, ma non voglio file separati.

risposta

23

$var non espande var in una stringa all'interno di gnuplot come farebbe in una shell. Si desidera utilizzare la concatenazione di gnuplot stringa (che utilizza l'operatore .):

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

O, suppongo che si possa utilizzare sprintf se si scopre che più a suo agio:

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

* Si noti che questo doesn' In realtà eseguo il comando. Costruisce semplicemente la stringa che verrà passata a plot e quindi plot eseguirà il comando. Se in realtà si desidera eseguire un comando in una funzione, è possibile chiamare system come una funzione. Dai documenti:

`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

Perfetto! Ora ho aggiunto anche un'altra funzione di supporto 'exec (cmd, arg1) = (system (sprintf (cmd, arg1)))' che mi esonera dal ridigitare 'system (sprintf())' diverse volte – Juve

+0

Ho appena visto che il mio 'exec' non funziona con' plot'. Come hai detto, 'plot' si aspetta una stringa, come prodotta dal tuo secondo snippet. Grazie per averlo indicato. – Juve