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

single quotes in gsub

833 views
Skip to first unread message

Da_Gut

unread,
Oct 19, 2009, 4:49:01 PM10/19/09
to
I need to replace the phrase " tells you, 'That'll be " (double quotes
don't actually exist) with a single tab. gsub works right up until
that ' . I haven't found a way to escape that single quote, not a \
not "'", not <\47>. This is easy enough to do with sed. Is this a
case of "don't be a purist" and prep the file with sed before feeding
it to awk?

Thanks!

pk

unread,
Oct 19, 2009, 5:06:16 PM10/19/09
to
Da_Gut wrote:

Unlike double quotes (which can appear escaped inside double quotes), single
quotes can't appear within single quotes, not even escaped.

You have several options here:

1) this is imho the cleanest option. Just put the awk script in a file.

$ cat foo.awk
{gsub(/hey 'joe'/,"hey 'bob'");print}

This way, you don't have the shell's single quotes in the way, and you can
use single quotes in your awk program freely.
Run the script using awk -f foo.awk datafile

2) Break out of awk and use a shell quote. There are various opinions about
this practice, which will probably explained by those who are in favour or
against it, so I won't go into the details here. (FWIW, I don't use it.)
However here it is for your information:

awk '{gsub(/hey '\''joe'\''/,"hey '\''bob'\''");print}'


3) Assign a variable the value ', and use it in the code:

awk -v sq=\' '{gsub("hey "sq"joe"sq,"hey "sq"bob"sq);print}'

but this introduces additional complications. Note that now you have to use
a string as the regex argument to gsub(). This creates a so-called computed-
regexp, which is somewhat more difficult to manage than a literal regexp (ie
one between slashes as in the first example), especially if it contains
double quotes or characters that are special in regular expressions.

See also this page:

http://www.gnu.org/manual/gawk/html_node/Quoting.html

Ed Morton

unread,
Oct 19, 2009, 5:50:04 PM10/19/09
to
pk wrote:
> Da_Gut wrote:
>
>> I need to replace the phrase " tells you, 'That'll be " (double quotes
>> don't actually exist) with a single tab. gsub works right up until
>> that ' . I haven't found a way to escape that single quote, not a \
>> not "'", not <\47>.

Try \047 instead, see below.

This is easy enough to do with sed. Is this a
>> case of "don't be a purist" and prep the file with sed before feeding
>> it to awk?

No.

> Unlike double quotes (which can appear escaped inside double quotes), single
> quotes can't appear within single quotes, not even escaped.
>
> You have several options here:
>
> 1) this is imho the cleanest option. Just put the awk script in a file.
>
> $ cat foo.awk
> {gsub(/hey 'joe'/,"hey 'bob'");print}
>
> This way, you don't have the shell's single quotes in the way, and you can
> use single quotes in your awk program freely.
> Run the script using awk -f foo.awk datafile
>
> 2) Break out of awk and use a shell quote. There are various opinions about
> this practice, which will probably explained by those who are in favour or
> against it, so I won't go into the details here. (FWIW, I don't use it.)
> However here it is for your information:
>
> awk '{gsub(/hey '\''joe'\''/,"hey '\''bob'\''");print}'
>
>
> 3) Assign a variable the value ', and use it in the code:
>
> awk -v sq=\' '{gsub("hey "sq"joe"sq,"hey "sq"bob"sq);print}'
>
> but this introduces additional complications. Note that now you have to use
> a string as the regex argument to gsub(). This creates a so-called computed-
> regexp, which is somewhat more difficult to manage than a literal regexp (ie
> one between slashes as in the first example), especially if it contains
> double quotes or characters that are special in regular expressions.

...or use \047:

$ echo "this 'quote' here" | awk '{gsub(/\047/,"|")}1'
this |quote| here

Not sure how portable that is.

Ed.

Ed Morton

unread,
Oct 20, 2009, 12:20:17 PM10/20/09
to

Looks like it's pretty portable:

$ echo "this 'quote' here" | nawk '{gsub(/\047/,"|")}1'
this |quote| here
$ echo "this 'quote' here" | /usr/xpg4/bin/awk '{gsub(/\047/,"|")}1'
this |quote| here
$ echo "this 'quote' here" | gawk '{gsub(/\047/,"|")}1'
this |quote| here

It failed on old, broken awk of course with the usual "syntax error
near line 1", but what doesn't....

Ed.

Da_Gut

unread,
Oct 20, 2009, 1:12:36 PM10/20/09
to

Excellent, another one to play with. At least I'm learning how to
escape stuff.....

0 new messages