AWK/shell quoting

Chuck Karish karish at forel.stanford.edu
Mon Jan 8 02:38:11 AEST 1990


In article <2367 at leah.Albany.Edu> emb978 at leah.Albany.EDU.UUCP
(Eric M. Boehm) wrote:
>In article <18067 at umn-cs.CS.UMN.EDU> mike at umn-cs.cs.umn.edu
>(Mike Haertel) writes:
>>In article <166 at omaha1.UUCP> wcc at omaha1.UUCP (William C Carey) writes:
>>>Any of the luminaries out there know how to get 'awk' (not nawk) to ouput a
>>>single-quote character ( ascii 0x27 ).
>>2.  You can do it by cooperating with shell quoting.  This is hairy,
>>but will print a single quote:
>>
>>	awk 'BEGIN {print "'"'"'"}'
>
>This solutions seem to be overly complex. Maybe I'm picking nits but
>wouldn't this be easier and simpler?
>
>awk 'BEGIN { print "\'" }'

Have you tried it?

    % awk 'BEGIN { print "\'" }'
    Unmatched ".
    % sh
    $ awk 'BEGIN { print "\'" }'
    > 
    > '
    > "
    awk: newline in string  }'... at source line 1
     context is
            BEGIN { print "\ }' >>> 
     <<< 
    awk: syntax error at source line 3
    awk: illegal statement at source line 3
            missing }


Here's what those nested quotes do:

    awk 'BEGIN {print "'"'"'"}'
                      1234321

    1) Usual quotes to set off a constant string for the 'print' directive
    2) Match the single quotes that set off the whole awk script; text
       within is interpreted by the shell.
    3) Quotes to protect the data from interpretation by the shell.
    4) The data, to be printed unchanged.

	Chuck Karish		karish at mindcraft.com
	(415) 323-9000		karish at forel.stanford.edu



More information about the Comp.unix.wizards mailing list