Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to print a literal ' in awk

938 views
Skip to first unread message

Janet Elise Rosenbaum

unread,
Aug 12, 1994, 3:42:33 PM8/12/94
to

well the subject says it all. I have tried several things inside of my
nawk scipt to attempt printing a single quote. here are some of them:

print "'"

print "\'"

print "\'\"

print "\\'"

printf("\'")

printf("\\'")

printf("\'\")

n="'"
printf("%s",$n)

n="\'"
printf("%s",$n)

The errors I get are very similar. They all look something like this:

**********

nawk: newline in string ).. at source line 2
context is
printf("\) >>>
<<<

nawk: syntax error at source line 3
nawk: illegal statement at source line 3
1 extra (

**********

this is in the case of my attempt to printf("\'")
the other error messages indicate essentially the same thing-- that a '
whether or not it is backslash escaped is interpreted as a special
character.

Is there any way around this?

Thanks.


--
Janet Rosenbaum: ejr...@merle.acns.nwu.edu, jro...@emu.acns.nwu.edu

Allen R Sparks

unread,
Aug 13, 1994, 3:45:23 AM8/13/94
to
>>>>> "J" == Janet Elise Rosenbaum <ejr...@merle.acns.nwu.edu> writes:

J> well the subject says it all. I have tried several things
J> inside of my nawk scipt to attempt printing a single quote.
J> here are some of them:

Well, using awk,
print "'"
worked for me. Of course, your subject line says "awk", and the body of
your post switches to "nawk". I don't have nawk, so I can't test it
there.

I'll add that I tested this inside a scriptfile with
%awk -f scriptfile infile
and this means that the shell's interpreter did not get a crack at it.
=== Al

Tim Maher/Consultix

unread,
Aug 13, 1994, 12:13:24 PM8/13/94
to
In article <FXARS.94A...@camelot.acf-lab.alaska.edu> you wrote:

Here is a simple technique that will work with NAWK or GAWK, but not AWK:

# example showing how to print a quote
# Tim Maher, Consultix, (206) 781-UNIX

nawk 'BEGIN{ print "\047"; exit}'

For AWK, you're not allowed to specify the character by its value in
the character set, so you're left with no recourse but to use a file for
the program, as shown above, or else use tricky quoting:

awk "BEGIN{print \"'\"; exit}"

For more info on this great language, attend our training classes in
Seattle, 8/22-24 !

--
Tim Maher, CONSULTIX (206) 781-UNIX INTERNET: t...@consultix.wa.com
-----------------------------------------------------------------------
Think UNIX training is expensive? Consider the words of our former VP:
"A mind is a terrible thing to waste, or not to have one." {8-)}

Mark Hughes

unread,
Aug 13, 1994, 10:08:12 AM8/13/94
to
In article <FXARS.94A...@camelot.acf-lab.alaska.edu>,

The same thing works in nawk. Nonetheless, it doesn't work if you are
using nawk in your script:

nawk 'BEGIN {print "'"}'

and not using the nawk -f scriptfile format.

According to my handy nutshell, there is an escape character \xhex that
should be able to print any character when the hex is replaced by the hex
value of the character, but I cannot get it to work.

Mark

--
Mark Hughes mhu...@nyx.cs.du.edu

"Managing is like holding a dove in your hand. Squeeze too hard and you
kill it; not hard enough and it flies away." -- Tommy Lasorda

Brian Springstead

unread,
Aug 13, 1994, 4:36:35 PM8/13/94
to
: : >>>>> "J" == Janet Elise Rosenbaum <ejr...@merle.acns.nwu.edu> writes:

: : J> well the subject says it all. I have tried several things
: : J> inside of my nawk scipt to attempt printing a single quote.
: : J> here are some of them:

: : Well, using awk,
: : print "'"
: : worked for me. Of course, your subject line says "awk", and the body of
: : your post switches to "nawk". I don't have nawk, so I can't test it
: : there.

: : I'll add that I tested this inside a scriptfile with
: : %awk -f scriptfile infile
: : and this means that the shell's interpreter did not get a crack at it.
: : === Al

Here's another way. It's no the easiest, but it works.

{ printf "Here is your single quote: %c\n", 39 } or for a multitude of them
try

BEGIN { s = sprintf("%c",39) }
{ print "That" s "s all, folks." }

Don't ya just love awk. Hope this helps.

--
Brian Springstead | Remember no man is a failure who has friends.
| Thanks for the wings!
bsp...@j51.com | Clarence

Ned Konz

unread,
Aug 13, 1994, 7:01:18 PM8/13/94
to
In article <ejrosen.776720553@merle>,

ejr...@merle.acns.nwu.edu (Janet Elise Rosenbaum) wrote:

> well the subject says it all. I have tried several things inside of my
> nawk scipt to attempt printing a single quote. here are some of them:

Are you having awk problems or shell problems? I didn't think that awk
considered a single quote as special.

You aren't running this as an embedded shell script, are you?

If you are, try changing to an all-awk file, run using "awk -f progname".

There are few reasons anymore that would require an in-line awk program.
After all, nawk allows "name=value" command line args that can be read
at FNR==1 time.

However, if you _must_ have an inline script, try something like this:

awk ' { printf('"\"abc'def\""'}'

As you can see, this begins to look a lot like line noise.


Alex Ugolini 4-6436 MRCE

unread,
Aug 14, 1994, 11:02:44 PM8/14/94
to
In article <1994Aug13.1...@consultix.wa.com> t...@consultix.wa.com writes:
>In article <FXARS.94A...@camelot.acf-lab.alaska.edu> you wrote:
>: >>>>> "J" == Janet Elise Rosenbaum <ejr...@merle.acns.nwu.edu> writes:

SUMMARY: how to print a single quote from within *awk:

[deleted...]

>: print "'"
[deleted...]


>: I'll add that I tested this inside a scriptfile with
>: %awk -f scriptfile infile

[deleted...]

>
> nawk 'BEGIN{ print "\047"; exit}'

[deleted]
> awk "BEGIN{print \"'\"; exit}"
>

I think this is a general solution, too:

who | awk '{print sq $0 sq}' sq="'" -
^^ ^^ ^^^^^^ ^
| | | |
| | | + read from stdin, required here
| | + set the awk variable "sq" to single-quote
+-----+-- print the variable sq around $0

------------------------------------------------------------------------------
Alex P. Ugolini, Jr. General Electric Medical Systems, Magnetic Resonance
ugol...@mr.med.ge.com Comments are my own, not necessarily my employer's.

Mark Brader

unread,
Aug 16, 1994, 11:39:31 PM8/16/94
to
[Corrected version; the first one was cancelled]

> However, if you _must_ have an inline script, try something like this:
> awk ' { printf('"\"abc'def\""'}'

For goodness' sake, this is not THAT hard. Do THIS a couple of times
and you'll get the hang of it. The important thing is to keep track
of which quoting mode you're in in sh. (You are using sh, aren't you?
Quoting is weird in csh. It finds an unbalanced ' in the above code,
whereas in sh, the quotes are balanced and awk sees the unbalanced "(".)

awk '{ ... print "abc'"'"'def" ... }'
\________________/\_/\__________/ quotes paired by shell
\___________/ quotes paired by awk
^ the quoted quote

If you have trouble with sh quoting, then put the awk script in a file
and just write a simple "abc'def" for the string.
--
Mark Brader "Yet again, I begged him to explain himself in plain
SoftQuad Inc. English. This request always surprises him, as he
m...@sq.com is always under the extraordinary impression that
Toronto he has done so." -- Lynn & Jay, "Yes Minister"

This article is in the public domain.

Johann Haider

unread,
Aug 17, 1994, 5:27:57 PM8/17/94
to
In <ejrosen.776720553@merle> ejr...@merle.acns.nwu.edu (Janet Elise Rosenbaum) writes:


>well the subject says it all. I have tried several things inside of my
>nawk scipt to attempt printing a single quote. here are some of them:

>print "'"

[more examples deleted]

>Is there any way around this?

yes there is one 8-)
This is more a shell question than an awk question and I have directed
followups to comp.unix.shells

The short answer is (tested with sh, csh and tcsh)

awk '{print "'"'"'"}'

you basically replace every ' in your string with the sequence '"'"'
An expert may give an explanation of this quoting mechanism

>Thanks.


hope it helps
Johann

--
Johann Haider Working Group on Rehabilitation Engineering
Institute of Electronics University of Technology Vienna, Austria
Email: j...@fortec.tuwien.ac.at phone: +431 504 18 30 31

0 new messages