1833059 Members
2832 Online
110049 Solutions
New Discussion

awk and quote

 
SOLVED
Go to solution
Nicolas Dumeige
Esteemed Contributor

awk and quote

Hello,

How do you protect the simple qote ' using awk's printf ?

I've tried using backslash, it doesn't seems to work properly ?

Thanks
All different, all Unix
10 REPLIES 10
Karthik S S
Honored Contributor

Re: awk and quote

Try,

printf "\""

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Nicolas Dumeige
Esteemed Contributor

Re: awk and quote

Not " but ' --> simple quote
All different, all Unix
Karthik S S
Honored Contributor

Re: awk and quote

If you prefer a dirty method you can use tr to substitute,

ex:

ls | awk -F " " '{printf "\"" $1}' | tr \" \'

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Nicolas Dumeige
Esteemed Contributor

Re: awk and quote

I can't belive that there is no way in awk to print a simple quote !
All different, all Unix
Karthik S S
Honored Contributor
Solution

Re: awk and quote

I found something interesting,

awk 'END {printf("Some '\''single'\'' quoted stuff\n");}'
Thanks to,
http://lists.slug.org.au/archives/slug/2002/11/msg00086.html

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Karthik S S
Honored Contributor

Re: awk and quote

My Above example works when I tried this,

ls | awk -F " " '{printf ("'\''''\''\n") $1}'

Too much complicated though ...!!!

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
Jean-Luc Oudart
Honored Contributor

Re: awk and quote

There is a way :

#!/bin/sh

QUOT="'"

echo " " | awk -v quot=$QUOT '{
printf("this is a quote %s\n",quot);
}'


Regards,
Jean-Luc
fiat lux
Nicolas Dumeige
Esteemed Contributor

Re: awk and quote

Thanks for you time :-D
All different, all Unix
Robert-Jan Goossens
Honored Contributor

Re: awk and quote

Hein van den Heuvel
Honored Contributor

Re: awk and quote

Pedantic perhaps, and perfectly obvious to some perhaps, but I'd like to point out that this has very little to do with awk.
It is a shell usage problem. The strict answer to the question is: Nothing: You do not need to protect a single quote in awk printf. Witness:

$ cat > x
BEGIN {print "test'"}
$ awk -f x
test'

Cheers,
Hein.