single quotes in gsub

  • Follow


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!
0
Reply Da_Gut 10/19/2009 8:49:01 PM

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>.  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?

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

0
Reply pk 10/19/2009 9:06:16 PM


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.

> See also this page:
> 
> http://www.gnu.org/manual/gawk/html_node/Quoting.html
> 

0
Reply Ed 10/19/2009 9:50:04 PM

On Oct 19, 4:50=A0pm, Ed Morton <mortons...@gmail.com> wrote:
> 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. =A0gsub 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.
>
> =A0 =A0This 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), s=
ingle
> > 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 a=
bout
> > 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=3D\' '{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 comp=
uted-
> > regexp, which is somewhat more difficult to manage than a literal regex=
p (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.
>

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.
0
Reply Ed 10/20/2009 4:20:17 PM

On Oct 20, 12:20=A0pm, Ed Morton <mortons...@gmail.com> wrote:
> On Oct 19, 4:50=A0pm, Ed Morton <mortons...@gmail.com> wrote:
>
>
>
>
>
> > pk wrote:
> > > Da_Gut wrote:
>
> > >> I need to replace the phrase " tells you, 'That'll be " (double quot=
es
> > >> don't actually exist) with a single tab. =A0gsub works right up unti=
l
> > >> that ' . I haven't found a way to escape that single quote, not a \
> > >> not "'", not <\47>.
>
> > Try \047 instead, see below.
>
> > =A0 =A0This is easy enough to do with sed. Is this a
>
> > >> case of "don't be a purist" and prep the file with sed before feedin=
g
> > >> 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 fil=
e.
>
> > > $ cat foo.awk
> > > {gsub(/hey 'joe'/,"hey 'bob'");print}
>
> > > This way, you don't have the shell's single quotes in the way, and yo=
u 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 favo=
ur or
> > > against it, so I won't go into the details here. (FWIW, I don't use i=
t.)
> > > 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=3D\' '{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 co=
mputed-
> > > regexp, which is somewhat more difficult to manage than a literal reg=
exp (ie
> > > one between slashes as in the first example), especially if it contai=
ns
> > > 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.
>
> 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....
>
> =A0 =A0 Ed.

Excellent, another one to play with. At least I'm learning how to
escape stuff.....
0
Reply Da_Gut 10/20/2009 5:12:36 PM

4 Replies
469 Views

(page loaded in 0.061 seconds)

Similiar Articles:













7/23/2012 1:13:37 AM


Reply: