using gsub on a string containing single quotes 
Author Message
 using gsub on a string containing single quotes

Thanks, Charles.  That's what I needed! -Neil

Quote:



> >I'm trying to use awk to change a single line in approx 1000 files.   The problem is that in
> >that line there is a term separated by single quotes and using \ as an escape does not work.

> >Normally I would do something like:

> >{
> >gsub (/cat/,"dog")
> >print
> >}

> >but here I have 'cat'

> >gsub (/\'cat\'/,"dog")  does not work.

> >How can I get awk to recognize the single quotes as simple text
> >characters?

> I suspect you're trying this from the command line, and not as
> an awk script, since they all work from a script involked as
> awk -f temp.awk infile

> So, from the command line, this is a quoting problem.

> Try this:

> awk '{gsub('"/'cat'/"', "dog"); print}' infile

> Chuck Demas
> Needham, Mass.

> --
>   Eat Healthy    |   _ _   | Nothing would be done at all,

>   Die Anyway     |    v    | That no one could find fault with it.


--
==================================
Neil Sandow, Pharm.D.

www.rxlist.com


Sat, 24 Aug 2002 03:00:00 GMT  
 using gsub on a string containing single quotes
I'm trying to use awk to change a single line in approx 1000 files.   The problem is that in
that line there is a term separated by single quotes and using \ as an escape does not work.

Normally I would do something like:

{
gsub (/cat/,"dog")
print

Quote:
}

but here I have 'cat'

gsub (/\'cat\'/,"dog")  does not work.

How can I get awk to recognize the single quotes as simple text characters?

Thanks!

-Neil


www.rxlist.com



Sun, 25 Aug 2002 03:00:00 GMT  
 using gsub on a string containing single quotes


Quote:
>I'm trying to use awk to change a single line in approx 1000 files.   The problem is that in
>that line there is a term separated by single quotes and using \ as an escape does not work.

>Normally I would do something like:

>{
>gsub (/cat/,"dog")
>print
>}

>but here I have 'cat'

>gsub (/\'cat\'/,"dog")  does not work.

>How can I get awk to recognize the single quotes as simple text
>characters?

I suspect you're trying this from the command line, and not as
an awk script, since they all work from a script involked as
awk -f temp.awk infile

So, from the command line, this is a quoting problem.

Try this:

awk '{gsub('"/'cat'/"', "dog"); print}' infile

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.



Sun, 25 Aug 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Substituting single quotes from Perl via shell using awk and gsub

2. single-quoted string conversion to triple-quoted string

3. Parsing a CSV file containing quoted strings (themselves containing commas)

4. gsub won't substitute single quotes

5. parsing lines with quoted strings as single fields

6. inserting single quotes inside a smallTalk string

7. Repost:Single quotes in string

8. Single quotes in string

9. Making strings with single/double quotes safe for MySQL Insert/Update

10. Putting single quotes inside Prolog strings

11. replace single quote to double quote with an example

12. gsub() replacement containing replaced text

 

 
Powered by phpBB® Forum Software