3

I need to pass a shell variable to awk in gnuplot but I get error messages : The variable is set in the sript and is called FILE. This changes according to date. My code : (in a Gnuplot script)

plot FILE using 1:14 with points pointtype 7 pointsize 1 # this works fine

replot '< awk ''{y1 = y2; y2 = $14; if (NR > 1 && y2 - y1 >= 100) printf("\n") ; if (NR > 1 && y2 -y1 <= -100) printf("\n"); print}'' FILE' using 1:14 with linespoints 
Err msg
awk: fatal: cannot open file `FILE' for reading (No such file or directory)

When I hard code the FILE path the replot works.

Could anyone clarify the code I need to pass this variable to awk? Am I on the right track with something like :

% environment_variable=FILE
% awk -vawk_variable="${environment_variable}" 'BEGIN { print awk_variable }' ?

Here is my Gnuplot script code: cobbled together from other posts mostly..

#FILE selection - we want to plot the most recent data file

FILE = strftime('/data/%Y-%m-%d.txt', time(0))  # this is correct

print "FILE is : " .FILE

#set file path variable for awk :      (This is where my problem is)

awk -v var="$FILE" '{print var}'

awk '{print $0}' <<< "$FILE"

Thank you in advance

5
  • In your first script, use "$FILE" instead of FILE. in your second, on the last line you don't need <<<. awk -v var="$FILE" '{print var}' is incomplete syntax and will hang your computer as it needs a file as argument. Just use awk '{print $0}' "$FILE" Commented Jul 18, 2014 at 8:58
  • @Ploutox He need the <<<, its get the data from a variable into awk with this. Commented Jul 18, 2014 at 8:59
  • @Jotne oh, I thought FILE just stored the path of the file, not its entire content. If that's the case then you are right indeed. EDIT: after further reading, it seems OP just stored the path of the most recent file in $FILE. He should then remove the <<< (otherwise his awk command will just print the path) Commented Jul 18, 2014 at 9:01
  • @Ploutox - you are correct - just the file path is stored in $FILE ; not the actual contents - these are parsed by Gnuplot Commented Jul 18, 2014 at 9:08
  • when I use : awk '{print $0}' "$FILE" (in my gnuplot script) I get an err msg : invalid command Commented Jul 18, 2014 at 9:40

1 Answer 1

2

If FILE is a gnuplot variable that contains the path of the file, you can do this:

FILE = 'input'
plot '<awk ''1'' ' . FILE

This concatenates the value of the gnuplot variable FILE onto the end of the awk command. The resulting awk "script" is therefore awk '1' input (which just prints every line of the file); you can substitute the '1' for whatever it is you want to do with awk.

By the way, your awk script can be simplified a little bit to this:

awk '{ y1 = y2; y2 = $14 } NR > 1 && (y2 - y1 >= 100 || y2 - y1 <= -100) { print "" } { print $1, $14 }'

It's not often that you need to use if in awk, as each block { } is executed conditionally (or if no condition is specified, the block is always executed). Assuming you haven't modified the record separator (the RS variable), print "" is the same as printf("\n"). Rather than specifying using 1:14 in gnuplot, you may as well only print the columns that you are interested in using print $1, $14.

So your replot line in gnuplot would be:

replot '<awk ''{ y1 = y2; y2 = $14 } NR > 1 && (y2 - y1 >= 100 || y2 - y1 <= -100) { print "" } { print $1, $14 }'' ' . FILE with linespoints

Of course, this line is getting a bit long. You might want to split it up a bit:

awk_cmd = '{ y1 = y2; y2 = $14 } NR > 1 && (y2 - y1 >= 100 || y2 - y1 <= -100) { print "" } { print $1, $14 }'
replot sprintf("<awk '%s' %s", awk_cmd, FILE) with linespoints
Sign up to request clarification or add additional context in comments.

4 Comments

Still struggling with this. I set FILE = strftime('/data/%Y-%m-%d.txt', time(0)) (in my gnuplot script)...To get this variable FILE into awk I am trying this : `awk -v 'var=FILE '{print var}'' .....is my syntax correct? AWk stubbornly refuses to read the variable and use it...Any help again much appreciated..
Do you want awk to process the file whose name is stored in the gnuplot variable FILE? If so, my answer tells you how to do that. If not, what exactly are you trying to do? Why would you want an awk script to print the value of a gnuplot variable? Please update your question to clarify. By the way, you still haven't accepted my answer to your previous question. I would appreciate it if you did.
Hi Tom - yes your initial answer seems correct - but I cannot get the code to work in my script. I am using awk because I need to do conditional plotting ie some data points must join and others do not based on their value. Here is the code + err msg: gnuplot> plot '< awk ''{y1 = y2; y2 = $12; if (NR > 1 && y2 - y1 >= 100) printf("\n") ; if (NR > 1 && y2 -y1 <= -100) printf("\n"); print}'. FILE using 1:12 with linespoints sh: -c: line 0: unexpected EOF while looking for matching `''sh: -c: line 1: syntax error: unexpected end of file warning: Skipping data file with no valid points
You haven't ended the awk command with ''. change print}'. FILE to print}'' '. FILE. Or better yet, use the code in my answer but change the $14s to $12s.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.