1

This command works well in a GNUplot script to find the line in a file with the value -1.98

system("/usr/bin/awk '/-1.98/{print $2 $1}' /path/to/myfile.txt")  

Given the GNUplot variable STATS_min_y resulting from a plot command, how would I pass STATS_min_y into that system() call in place of the -1.98 value?

system("/usr/bin/awk '/STATS_min_y/{print $2 $1}' /path/to/myfile.txt")  

and similar variants fail with a demand for a missing parenthesis.

Dan
  • 527
  • 1
  • 7
  • 25
  • 2
    You probably want something like `system("/usr/bin/awk -v value=-1.98 '$0 ~ value {print $2 $1}' /path/to/myfile.txt")` – fedorqui Jun 18 '15 at 22:10
  • 2
    Using whatever string concatenation support GNUplot has to construct that command string. – Etan Reisner Jun 18 '15 at 22:14
  • The concat operator for GNUplot is the period, and this works `system("/usr/bin/awk -v value= ". lowValue . " '$0 ~ value {print $2 $1}' /path/to/myfile.txt")` in the sense that it recognizes the value of the variable (lowValue) but fails with this error: `unknown option -1.98 ignored`, and returns every line of the file. – Dan Jun 19 '15 at 02:11
  • There must be no space after `value=`. – Christoph Jun 19 '15 at 05:15
  • Not sure if my guess is right: [Gnuplot: how to plot max and /or min value](http://stackoverflow.com/q/30130639/2604213) – Christoph Jun 19 '15 at 07:00
  • I'm using GNUplot's STATS_min_y variable to find the lowest value in a file and then need to extract other values from that line. `lowValue = sprintf (" %g", STATS_min_y);`gives me that number in a format I can use, and the command `system("/usr/bin/awk -v value=". lowValue." '$0 ~ value {print $2 $1}' /path/to/myfile.txt")` recognizes the value of the variable but still throws that error about `unknown option`which I guess must mean I have the awk formatting wrong. Though that is odd as the command works properly if the value itself (-1.98) is used as suggested by @fedorqui – Dan Jun 19 '15 at 13:50
  • Update: the culprit is the %g format specifier, which is passing a number not a string, to awk. Changed to `lowValue = sprintf (" %s", STATS_min_y);` and all works as expected. – Dan Jun 19 '15 at 14:57
  • have you checked the variable STATS_pos_min_y? I guess it might already contain the value you're after. – Karl Jun 20 '15 at 20:59
  • @karl I had missed that, thank you. – Dan Jun 21 '15 at 21:55

1 Answers1

0

You can pass the result from sprintf directly to system. For example:

a = system( sprintf("awk '/%g/{print $2,$1}' myfile.txt", STATS_min_y) )
vagoberto
  • 2,372
  • 20
  • 30