Thanks!
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:
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.
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.
Excellent, another one to play with. At least I'm learning how to
escape stuff.....