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: single quotes in gsub - comp.lang.awkI 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 ... Data with "," and field sepeator is ",", How to handle this - comp ...single quotes in gsub - comp.lang.awk Data with "," and field sepeator is ",", How to handle this - comp ..... 3 == STR ) { print INLINE } else { for (i=1;i<=6;i++) { gsub ... GNU-awk bug in sub()/gsub() - comp.lang.awksingle quotes in gsub - comp.lang.awk Same note about escaping quotes would apply to this example ... If: 1. You have the C source for an md5sum function, 2. ... How to insert a single literal quote (") character in a string ...single quotes in gsub - comp.lang.awk Just put the awk script in a file. $ cat foo ... Note that now you have to use a string as the ... lang.awk... html_node/Quoting.html ... question about gsub - comp.lang.awksingle quotes in gsub - comp.lang.awk... Post Question | Groups | ... $ cat foo.awk {gsub(/hey 'joe'/,"hey 'bob'");print} This way, you don't have the ... Braces within a string - comp.lang.awksingle quotes in gsub - comp.lang.awk Braces within a string - comp.lang.awk... string enclosed in braces (i.e., {JOE IS NOT HERE}) I am having trouble using gsub ... How To Use Substitute To Replace Double Quotes - comp.databases ...single quotes in gsub - comp.lang.awk How To Use Substitute To Replace Double Quotes - comp.databases ... single quotes in gsub - comp.lang.awk I need to replace the ... Replacing a backslash with a forward slash in Javascript - comp ...single quotes in gsub - comp.lang.awk I need to replace the phrase " tells you, 'That'll be ... r.t POSIX specification at least) involving backslash ... in gsub has its ... use of W&Z reg in 8085 - comp.dspsingle quotes in gsub - comp.lang.awk This creates a so-called co= mputed- > > > regexp, which is somewhat more difficult to manage than a literal reg= exp (ie > > > one ... awk: bailing out near line 1 - comp.lang.awksingle quotes in gsub - comp.lang.awk Run the script using awk -f foo.awk datafile 2) Break out of awk and ... this |quote| here It failed on old, broken awk of course ... single quotes in gsub - comp.lang.awk | Computer GroupI 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 ... string - Ruby gsub replace single quote with backslash single ...(the actual string is very long and contains many single quotes) I'm trying to this by using gsub on the string, but can't get this to work. a = "Cote d'Ivoir" a.gsub("'", "\\\'") 7/23/2012 1:13:37 AM
|