[net.unix] Quoting quotes in awk

ken@turtlevax.UUCP (Ken Turkowski) (03/04/86)

How does one quote quotes in awk?  I'd like to do something like the following:

	print doit "\"" last0 "\"", "\"" $0 "\"" | "sh"

I.e. I want to pipe output to a program, putting quotes around some of
the arguments so they don't get interpreted by the shell.  Alternatively,
I'd like to execute the program "doit" with arguments determined from
the awk program:

	print "" | doit "\"" last0 "\"", "\"" $0 "\""

Can someone give me some ideas on how to do this?
-- 
Ken Turkowski @ CIMLINC, Menlo Park, CA
UUCP: {amd,decwrl,hplabs,seismo,spar}!turtlevax!ken
ARPA: turtlevax!ken@DECWRL.DEC.COM

mo@wgivax.UUCP (03/05/86)

>From ken@turtlevax.UUCP (Ken Turkowski) Sun Feb  6 01:28:16 206

>How does one quote quotes in awk?  I'd like to do something like the following:

>	print doit "\"" last0 "\"", "\"" $0 "\"" | "sh"

>Alternatively, I'd like to execute the program "doit" with arguments
>determined from the awk program:

>	print "" | doit "\"" last0 "\"", "\"" $0 "\""

execute 'awk -f <filename>' where file <filename> has following awk commands:

BEGIN {Q = "\""}
NR > 1 {printf("doit %s %s %s %s %s %s | sh\n",Q,last,Q,Q,$0,Q)}
NR > 1 {printf("%s%s | doit %s %s %s %s %s %s\n",Q,Q,Q,last,Q,Q,$0,Q)}
{last = $0}

note that this could also be done directly in sh, and, depending on exactly
what you are trying to do, you might be able to save some steps in getting
the command generated by "doit" to execute.  following is an example of
how to do it in sh:

lastline=""
read line
while (test $line)
do
	if(test $lastline)
	then
		echo 'doit " '$lastline' " " '$line' " | sh'
	fi
	lastline=$line
	read line
done

note that this script expects input to be given to it

chris@umcp-cs.UUCP (03/07/86)

I just tried the following on a 4.3BSD beta system and it worked fine:

	% awk -f /dev/stdin
	{ printf("%d: \"%s\"\n", NR, $0); }
	control-D hi
	1: "hi"
	there
	2: "there"
	control-D %

So what is the problem with quotes?
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 1415)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@mimsy.umd.edu

ken@turtlevax.UUCP (Ken Turkowski) (03/12/86)

In article <139@umcp-cs.UUCP> chris@umcp-cs.UUCP (Chris Torek) writes:
>I just tried the following on a 4.3BSD beta system and it worked fine:
>
>	% awk -f /dev/stdin
>	{ printf("%d: \"%s\"\n", NR, $0); }
>	control-D hi
>	1: "hi"
>	there
>	2: "there"
>	control-D %
>
>So what is the problem with quotes?

You're right; it does work.  My problem was that I was trying to feed the
program into sh on the command line.  A permutation of it is this:

awk '{ print "quoth the raven:", "'\''" $0 "'\''", "(nevermore)" }' << EOF
what do you say?
<to quote a quote>
EOF

Which results in:
quoth the raven: 'what do you say?' (nevermore)
quoth the raven: '<to quote a quote>' (nevermore)

The problem starts because I want the enclose the entire awk program in
single quotes to avoid expansion of things like $0; additionally, I
would like to print a single quote in an awk print statement.  The
solution is outlined below:

awk '{ print "quoth the raven:", "'\''" $0 "'\''", "(nevermore)" }'
    ^				  ^  ^      ^  ^		  ^
    |		first string	  |  |2ndStr|  |   third string	  |
    +-----------------------------+  +------+  +------------------+
				    ^	      ^
				    |	      |
				escaped single quotes

One of the net readers had the good suggestion of setting a variable
to equal the single quote in a string, for greater readability:

awk 'BEGIN { Q = "'\''" }
{ print "quoth the raven:", Q $0 Q, "(nevermore)" }'

Thanks to everyone who responded!
-- 
Ken Turkowski @ CIMLINC, Menlo Park, CA
UUCP: {amd,decwrl,hplabs,seismo,spar}!turtlevax!ken
ARPA: turtlevax!ken@DECWRL.DEC.COM