Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site turtlevax.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!decwrl!turtlevax!ken From: ken@turtlevax.UUCP (Ken Turkowski) Newsgroups: net.unix Subject: Re: Quoting quotes in awk (really quoting quotes in sh) Message-ID: <1082@turtlevax.UUCP> Date: Tue, 11-Mar-86 20:28:25 EST Article-I.D.: turtleva.1082 Posted: Tue Mar 11 20:28:25 1986 Date-Received: Fri, 14-Mar-86 04:48:56 EST References: <1074@turtlevax.UUCP> <152@wgivax.UUCP> <139@umcp-cs.UUCP> Reply-To: ken@turtlevax.UUCP (Ken Turkowski) Organization: CIMLINC, Inc. @ Menlo Park, CA Lines: 49 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? EOF Which results in: quoth the raven: 'what do you say?' (nevermore) quoth the raven: '' (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