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
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>...
http://web.cs.mun.ca/~michael/c/ascii-table.html
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
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
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
> 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