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
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 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-)}
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
: : 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
> 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.
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.
> 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.
>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.
>--
>Janet Rosenbaum: ejr...@merle.acns.nwu.edu, jro...@emu.acns.nwu.edu
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