1

I'd like to use gnuplot with awk in shell command line like below.

gnuplot -persist -e " plot '< awk_command' "

My awk command is

awk '/match_pattern/ {print $4}' log.txt

I cannot find how to handle quotes inside the my awk command.

gnuplot -persist -e "plot " < awk '/matchpattern/ {print $4}' log.txt " "

This produced an error message that

awk: No such file or directory.

When I did this in gnuplot, there was no problem.

gnuplot> plot " < awk '/matchpattern/ {print $4}' log.txt "

I want to plot it in shell command line not in gnuplot just for convenience.

2 Answers 2

1

From the Gnuplot documentation:

Command-line substitution is specified by a system command enclosed in backquotes. This command is spawned and the output it produces replaces the backquoted text on the command line

source: GnuPlot 5.0 documentation

So this can easily be tested in gnuplot itself:

gnuplot> `awk 'BEGIN{print "plot sin(x)"}'`

If you now want to use this with the -e flag from the command-line, it should look something like this:

$ gnuplot --persist -e "`awk 'BEGIN{print \"plot sin(x)\"}' ` "

Notice that we had to escape the <double-quotes> inside the awk command. This is due to bash quoting rules. If you do not want to escape the double quotes, you will have to use single quotes and escape these again. That will than look like this:

$ gnuplot --persist -e '`awk '"'"'BEGIN{print "plot sin(x)"}'"'"' ` '

If you use awk just to generate the data and you want to use the pipe system of gnuplot:

On systems with a popen function, the datafile can be piped through a shell command by starting the file name with a ’<’. For example,

pop(x) = 103*exp(-x/10)
plot "< awk '{print $1-1965, $2}' population.dat", pop(x)

source: GnuPlot 5.0 documentation

So in case of the OP, with the proper escaping, you would do:

$ gnuplot --persist -e "plot \"< awk '/matchpattern/{print \$4}' log.txt \""

notice how I also had to escape the $, otherwise, bash would have substituted it with the 4th bash argument which is most likely an empty string.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much!! Escaping quotes and $ was the key. I was using csh actually and found that escaping $ is inconvenient. Furthermore I needed to put multiple files to awk, which meant I need to use wildcards '*'. This made the command more complicated. And I think this is out of scope of this question. Anyway, your answer works in bash very well. Thank you again!!!
0

You just need to escape the nested double quotes:

gnuplot -persist -e "plot \" < awk '/matchpattern/ {print $4}' log.txt \" "

If you don't do this your shell will interpret as separate arguments for gnuplot

  • -e "plot "
  • < awk
  • '/matchpattern/ {print $4}'
  • log.txt

and the shell will wrongly think that it should redirect the standard input of your gnuplot command from a file called awk. As the file does not exist in your working directory it throws the error: awk: No such file or directory.

2 Comments

Thanks for your kindness. This time the error message is : Unmatched ". My gnuplot version is 4.2 patchlevel 6. Will this matters?
This is what I tested. gnuplot -persist -e "plot \" < awk '/^JOB.*Time/ {print $4}' log.txt \" "

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.