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

printing single quote (apostrophe) with awk

610 views
Skip to first unread message

jiml...@my-dejanews.com

unread,
Oct 20, 1998, 3:00:00 AM10/20/98
to
I am relatively new to awk; forgive me if this is too basic:
Is there a way to have awk print a single quote? i.e. what I want to do is:

echo hello | awk '{print "\'" $1 "\'"}'
and get output of 'hello'
(thinking than escaping the single quote will work - but it doesn't)

Can anyone help?

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum

Cesar Mugnatto

unread,
Oct 20, 1998, 3:00:00 AM10/20/98
to
Use \047 for single quote (incidentally \042 is for double quotes)

It is better used in the printf rather than print statement as in

printf "%c%s%c", '\047', $1, '\047'

Cesar
--
Please remove the UPPERCASE characters from my e-mail address for the real
thing
jiml...@my-dejanews.com wrote in message
<70ir5g$9eo$1...@nnrp1.dejanews.com>...

Cesar Mugnatto

unread,
Oct 20, 1998, 3:00:00 AM10/20/98
to
P.S. Hopefully this guy keeps his page up because I refer to it constantly
when I need to find an ascii code:

http://web.cs.mun.ca/~michael/c/ascii-table.html

Patrick TJ McPhee

unread,
Oct 21, 1998, 3:00:00 AM10/21/98
to
In article <70ir5g$9eo$1...@nnrp1.dejanews.com>,
<jiml...@my-dejanews.com> wrote:
% I am relatively new to awk; forgive me if this is too basic:
% Is there a way to have awk print a single quote? i.e. what I want to do is:

This isn't an awk question.

% echo hello | awk '{print "\'" $1 "\'"}'

understand that you're using the shell to pass a program to awk. What
awk sees here is
{print "\

The reason being that the shell is screwing things up (or alternately
it's doing exactly what you told it to, take your pick). You need to
get awk to see this:
{ print "'" $1 "'" }

One way to do that is to use " as the shell quote character, and escape
$ and ":

echo hello | awk "{print \"'\" \$1 \"'\"}"

Another is to use ' as the shell quote character, and escape ' to the shell
where needed:

echo hello | awk '{print "'\''" $1 "'\''"}'

Another is to use both " and ' as shell quote characters:

echo hello | awk '{print "'"'"'" $1 "'"'"'"}'

And another is to use back-slashes to escape everything that's special
to the shell:
echo hello | awk \{print\ \"\'\"\ \$1\ \"\'\"\}

Another is to put the awk script in a file and include it using the -f option.
All the nonsense with quoting things comes from putting the program on
the command-line.
--

Patrick TJ McPhee
East York Canada
pt...@interlog.com

L. M. Schmitt

unread,
Oct 22, 1998, 3:00:00 AM10/22/98
to jiml...@my-dejanews.com
> Is there a way to have awk print a single quote? i.e. what I want to do is:
>
> echo hello | awk '{print "\'" $1 "\'"}'
> and get output of 'hello'

this is a shell problem, rather than an awk problem:

solution A:
========
use of awk within e.g. the Bourne shell
awk '{print "`" $1 "'\''"}'
concatenates the 3 strings
{print "`" $1 "
\' == ' for sh (escaped by \)
"}
in the Bourne shell to a string==program for akw
namely
solution B:
============
{print "`" $1 "'"}
which is what one would put into a awk-program file.

yields `hello' in your example

not tested
LMS
free sed/awk book:
ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.ps.gz
ftp://ftp.u-aizu.ac.jp/u-aizu/doc/Tech-Report/1997/97-2-007.tar.gz

Charles Demas

unread,
Oct 22, 1998, 3:00:00 AM10/22/98
to Patrick TJ McPhee
In article <70jnmu$7ai$1...@news.interlog.com>,

Patrick TJ McPhee <pt...@interlog.com> wrote:
>In article <70ir5g$9eo$1...@nnrp1.dejanews.com>,
> <jiml...@my-dejanews.com> wrote:
>% I am relatively new to awk; forgive me if this is too basic:
>% Is there a way to have awk print a single quote? i.e. what I want to do is:
>
>This isn't an awk question.
>
>% echo hello | awk '{print "\'" $1 "\'"}'
>
>understand that you're using the shell to pass a program to awk. What
>awk sees here is
> {print "\
>

While you could call it a quoting question, you failed to mention one
simple awk method that's on the old awk man page, use sprintf to
define a variable, and then use the variable. Something like this:

echo hello|awk 'BEGIN{sq=sprintf("%c",39)} {print sq $1 sq}'

The old awk man page finds this as a reasonable way to print double quotes.
The man page suggests dq=sprintf("%c",34) and that awk (at least the
old one) had problems trying to print double quotes.

It seems relatively straightforward to me, and makes all that escaping
and quoting hassle go away.

It also make it an awk answer to the question. :-)


Chuck Demas
Needham, Mass.

>The reason being that the shell is screwing things up (or alternately
>it's doing exactly what you told it to, take your pick). You need to
>get awk to see this:
> { print "'" $1 "'" }
>
>One way to do that is to use " as the shell quote character, and escape
>$ and ":
>
> echo hello | awk "{print \"'\" \$1 \"'\"}"
>
>Another is to use ' as the shell quote character, and escape ' to the shell
>where needed:
>
> echo hello | awk '{print "'\''" $1 "'\''"}'
>
>Another is to use both " and ' as shell quote characters:
>
> echo hello | awk '{print "'"'"'" $1 "'"'"'"}'
>
>And another is to use back-slashes to escape everything that's special
>to the shell:
> echo hello | awk \{print\ \"\'\"\ \$1\ \"\'\"\}
>
>Another is to put the awk script in a file and include it using the -f option.
>All the nonsense with quoting things comes from putting the program on
>the command-line.
--

Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
de...@tiac.net | \___/ | http://www.tiac.net/users/demas

Mariano Corral

unread,
Oct 22, 1998, 3:00:00 AM10/22/98
to
Charles Demas <de...@sunspot.tiac.net> wrote:

> While you could call it a quoting question, you failed to mention one
> simple awk method that's on the old awk man page, use sprintf to
> define a variable, and then use the variable. Something like this:
>
> echo hello|awk 'BEGIN{sq=sprintf("%c",39)} {print sq $1 sq}'
>

I prefer too defining a variable. At HP-UX, I do it this way

echo hello|awk -v SINGLE=\' -v DOUBLE=\" '{print SINGLE $1 DOUBLE "..."}'

Mariano Corral


-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

0 new messages