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.
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.