Substituting single quotes from Perl via shell using awk and gsub 
Author Message
 Substituting single quotes from Perl via shell using awk and gsub

Hi All,
Chuck describes below how to fanagle substitutions with a search
pattern containing single quotes.
How does one "substitute" in single quotes into the replacement
pattern.
Especially in my case where I'm placing this in a system call from
within Perl--> you see it gets complicated with all the underlying
quoting and escaping!!
My example is:
system( " echo $sent | awk '{gsub('"/^/"', " ' "); print}' |
/a/infofa/h/users/gchapman/experiment/system/main-vanilla-client prg1
17253 2>/dev/null > perl.fdg");

where $sent is a var which contains a text sentence surrounded by
double quotes. It itself has a double quoted portion inside it.
My intent is to replace all ^ with ' inside the sentence.

Thanks all.
    ...George
Search Result 1

Subject: Re: using gsub on a string containing single quotes
View: Complete Thread (3 articles)  
Original Format
Newsgroups: comp.lang.awk
Date: 2000/03/08



Quote:
>I'm trying to use awk to change a single line in approx 1000 files.  

The problem is that in
Quote:
>that line there is a term separated by single quotes and using \ as

an escape does not work.

Quote:

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



Wed, 23 Mar 2005 11:42:09 GMT  
 Substituting single quotes from Perl via shell using awk and gsub
On 4 Oct 2002 20:42:09 -0700,

Quote:
> Hi All,
> Chuck describes below how to fanagle substitutions with a search
> pattern containing single quotes.
> How does one "substitute" in single quotes into the replacement
> pattern.
> Especially in my case where I'm placing this in a system call from
> within Perl--> you see it gets complicated with all the underlying
> quoting and escaping!!
> My example is:
> system( " echo $sent | awk '{gsub('"/^/"', " ' "); print}' |
> /a/infofa/h/users/gchapman/experiment/system/main-vanilla-client prg1
> 17253 2>/dev/null > perl.fdg");

> where $sent is a var which contains a text sentence surrounded by
> double quotes. It itself has a double quoted portion inside it.
> My intent is to replace all ^ with ' inside the sentence.

In addition to the single quote problems, remember that ^ in a regular
expression represents the beginning, so you need to escape it.  Are you
sure you need to use awk inside a perl script?  You could use tr to change
all ^ to '.


Wed, 23 Mar 2005 12:56:30 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:
> In addition to the single quote problems, remember that ^ in a regular
> expression represents the beginning, so you need to escape it.  Are you
> sure you need to use awk inside a perl script?  You could use tr to change
> all ^ to '.

Thanks Bill.
Yes I do need to use some unix command from within Perl since I'm
using Perl for my main control and processing but then need to run a
number of scripts outside of Perl. But my problem is how /when the
silly metacharacters are interpreted.
I.E. $sent above has a single quote in it so I need to change it to
something to get past all covert substitutions so that in Unix shell I
can change it back to a single quote ...but even then it gets
"trapped".
I've tried tr and still have similar problem.
Thx
George


Wed, 23 Mar 2005 21:18:12 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:

>> In addition to the single quote problems, remember that ^ in a regular
>> expression represents the beginning, so you need to escape it.  Are you
>> sure you need to use awk inside a perl script?  You could use tr to change
>> all ^ to '.

>Thanks Bill.
>Yes I do need to use some unix command from within Perl since I'm
>using Perl for my main control and processing but then need to run a
>number of scripts outside of Perl. But my problem is how /when the
>silly metacharacters are interpreted.
>I.E. $sent above has a single quote in it so I need to change it to
>something to get past all covert substitutions so that in Unix shell I
>can change it back to a single quote ...but even then it gets
>"trapped".
>I've tried tr and still have similar problem.
>Thx
>George

Well, if you say you have, then you have to, but it sure sounds dumb to us.

It's a lot like when you ask in a Perl group how to do X without using Y,
and all they can tell you is, "Well, first you use Y and then everything
works fine".

Well, sorta like that, anyway...



Wed, 23 Mar 2005 23:01:44 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:
> Hi All,
> Chuck describes below how to fanagle substitutions with a search
> pattern containing single quotes.
> How does one "substitute" in single quotes into the replacement
> pattern.
> Especially in my case where I'm placing this in a system call from
> within Perl--> you see it gets complicated with all the underlying
> quoting and escaping!!
> My example is:
> system( " echo $sent | awk '{gsub('"/^/"', " ' "); print}' |
> /a/infofa/h/users/gchapman/experiment/system/main-vanilla-client prg1
> 17253 2>/dev/null > perl.fdg");

> where $sent is a var which contains a text sentence surrounded by
> double quotes. It itself has a double quoted portion inside it.
> My intent is to replace all ^ with ' inside the sentence.

> Thanks all.
>     ...George
> Search Result 1

> Subject: Re: using gsub on a string containing single quotes
> View: Complete Thread (3 articles)
> Original Format
> Newsgroups: comp.lang.awk
> Date: 2000/03/08



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

You know, in this problem, you only have to worry about shell quoting and
AWK quoting.  It seems more complicated because you are using Perl
interpolated strings, using double-quotes.  The 'qq' operator, as I recall,
let's you use any quoting character you want, including bracing sets, like
{}, and maybe [], (), maybe even <>.  That would save some internal
escaping.

But, let's stay with your double-quote interpolation...

You just have to take it step by step, and make sure you understand all of
the quoting steps involved--beginning with how you are mangling your initial
data.

I don't understand it when you say, "$sent is a var which contains a text
sentence surrounded by double quotes. It itself has a double quoted portion
inside it. My intent is to replace all ^ with ' inside the sentence."  I
will assume that the first double quotes to hich you refer are really part
of the value, not part of the quoting of the string in your Perl program.

Would this be an example of the value of $sent?

    "He says, "Hi!" Then he realises what^s happeneing."

or are there also single quotes also, like

    "He says, "Hi!" Then he realises ^what's^ happeneing."

If I assigned this into an AWK variable, would it look like this?

    sent = "\"He says, \"Hi!\" Then he realises ^what's^ happeneing.\""

To sub the "^" caharacters into "'" characters, you'd use

    {gsub(/\^/,"'");print}

To quote-out this awk program on a command line for my shell (bash), it
could look like this:

    awk '{gsub(/\^/,"'"'"'");print}'

which is three parts:

    awk '{gsub(/\^/"'
    "'"
    '");print}'

So now, if I wanted to echo that previously discussed string out, I say:

    echo '"He says "Hi!" Then he realizes ^what'"'"'s^ happening."'

Then my command

    echo '"He says "Hi!" Then he realizes ^what'"'"'s^ happening."' | \
    awk '{gsub(/\^/,"'"'"'");print}'

yeilds

    "He says "Hi!" Then he realizes 'what's' happening."

You have that original text in the Perl variable $sent, only, it has NOT
been broken down in such a way that you can just

    system("echo $sent")

You have to quote-out all the quotes within $sent for bash, the same way I
did with that awk command.  Within $sent, you have to replace every
    '
with
    '"'"'  (end of one single-quoted string, then "'", then start of
another)
And you must then add a leading and trailing
    '

In AWK, I'd do

    sent = "\"He says, \"Hi!\" Then he realises ^what's^ happeneing.\""
    gsub(/'/,"'\"'\"'",sent)
    sent = "'" sent "'"

I'll leave it as an excercise to translate this to Perl.

Next you have to figure out how to properly quote the bash command line for
awk:

    awk '{gsub(/\^/,"'"'"'");print}'

by escaping the internal double quotes, and the escape character itself,
becomes the AWK string

    cmd = "awk '{gsub(/\\^/,\"'\"'\"'\");print}'"

or interpolated Perl string

    $cmd = "awk '{gsub(/\\^/,\"'\"'\"'\");print}'"

Now we are at the point where we can build the system() call.  Let's put the
rest of that awful long command into a string that doesn't have any quoting
issues (but, alas, it has wrapping issues):

    $prg = '/a/infofa/h/users/gchapman/experiment/system/main-vanilla-client
prg117253 2>/dev/null > perl.fdg'

Then just say:

    system("echo $sent | awk '{gsub(/\\^/,\"'\"'\"'\");print}' | $prg")

    - Dan



Thu, 24 Mar 2005 03:06:06 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:

> Well, if you say you have, then you have to, but it sure sounds dumb to us.

> It's a lot like when you ask in a Perl group how to do X without using Y,
> and all they can tell you is, "Well, first you use Y and then everything
> works fine".

> Well, sorta like that, anyway...

Potentially good advice but how else would I do it? I'm open ;-)
Even if someone can tell me how to use 'tr' in such a convoluted way
it would be appreciated.
Thx
...George


Thu, 24 Mar 2005 03:25:37 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:

> > In addition to the single quote problems, remember that ^ in a regular
> > expression represents the beginning, so you need to escape it.  Are you
> > sure you need to use awk inside a perl script?  You could use tr to change
> > all ^ to '.

> Thanks Bill.
> Yes I do need to use some unix command from within Perl since I'm
> using Perl for my main control and processing but then need to run a
> number of scripts outside of Perl. But my problem is how /when the
> silly metacharacters are interpreted.
> I.E. $sent above has a single quote in it so I need to change it to
> something to get past all covert substitutions so that in Unix shell I
> can change it back to a single quote ...but even then it gets
> "trapped".
> I've tried tr and still have similar problem.
> Thx
> George

Untested, but this is probably more likely to work than your first
attempt ...!

system( "echo $sent | awk -v sq=\"'\" '{gsub('\\^',sq); print}' |
/a/infofa/h/users/gchapman/experiment/system/main-vanilla-client prg1
17253 2>/dev/null > perl.fdg");

(Depending on version of awk, you may need to use an option other than
-v to set-up the sq variable).

Mark.



Thu, 24 Mar 2005 05:21:50 GMT  
 Substituting single quotes from Perl via shell using awk and gsub
Thanks Dan please see inline.

Quote:
> or are there also single quotes also, like

>     "He says, "Hi!" Then he realises ^what's^ happeneing."

yes this is what I'm talking about

I've tried a small experiment based on what you said just to reassure
myself before I jump into the Perl twillight zone. Here it is:
------------------------
{site0}gchapman(37)$ echo '"he says "Hi!" Then he realizes
^what'"'"'s^ happening."' | awk '{gsub(/\\^/, \"'\"'\"'\");print}'
bash: syntax error near unexpected token `;'

------------------------------------
As you can see it was unsucessful. I'm not sure what's happening now.
Any idea. It looks like bash is complaining.

Thanks again for{*filter*} in there with me.
BTW how would I use TR? would it be any easier? keep in mind I'm not a
unix guru.(as you can see ;-)
Thx

George



Fri, 25 Mar 2005 08:52:32 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:
> Thanks Dan please see inline.

> > or are there also single quotes also, like

> >     "He says, "Hi!" Then he realises ^what's^ happening."
> yes this is what I'm talking about

> I've tried a small experiment based on what you said just to reassure
> myself before I jump into the Perl twilight zone. Here it is:
> ------------------------
> {site0}gchapman(37)$ echo '"he says "Hi!" Then he realizes
> ^what'"'"'s^ happening."' | awk '{gsub(/\\^/, \"'\"'\"'\");print}'
> bash: syntax error near unexpected token `;'

> ------------------------------------
> As you can see it was unsuccessful. I'm not sure what's happening now.
> Any idea. It looks like bash is complaining.

> Thanks again for{*filter*} in there with me.
> BTW how would I use TR? would it be any easier? keep in mind I'm not a
> unix guru.(as you can see ;-)
> Thx

> George

George,

Dan's "fully-quoted/escaped" version was intended for use inside a
perl/awk script.  At the command line prompt what you need is:

$ echo '"he says "Hi!" Then he realizes
^what'"'"'s^ happening."' | awk '{gsub(/\^/, "'"'"'");print}'
"he says "Hi!" Then he realizes
'what's' happening."

You should get the result in the last two lines if the quoting's OK.

Peter
--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Fri, 25 Mar 2005 09:47:00 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

[...]

Quote:

> system( "echo $sent | awk -v sq=\"'\" '{gsub('\\^',sq); print}' |
> /a/infofa/h/users/gchapman/experiment/system/main-vanilla-client prg1
> 17253 2>/dev/null > perl.fdg");

> (Depending on version of awk, you may need to use an option other than
> -v to set-up the sq variable).

Not if it's a vendor-supplied POSIX compliant awk, mawk, BWK's awk or
gawk.  These should all use -v to set a variable for use within, or
after, the BEGIN block.

--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Fri, 25 Mar 2005 10:28:44 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:
> Thanks Dan please see inline.

> > or are there also single quotes also, like

> >     "He says, "Hi!" Then he realises ^what's^ happeneing."
> yes this is what I'm talking about

> I've tried a small experiment based on what you said just to reassure
> myself before I jump into the Perl twillight zone. Here it is:
> ------------------------
> {site0}gchapman(37)$ echo '"he says "Hi!" Then he realizes
> ^what'"'"'s^ happening."' | awk '{gsub(/\\^/, \"'\"'\"'\");print}'
> bash: syntax error near unexpected token `;'

> ------------------------------------
> As you can see it was unsucessful. I'm not sure what's happening now.
> Any idea. It looks like bash is complaining.

> Thanks again for{*filter*} in there with me.
> BTW how would I use TR? would it be any easier? keep in mind I'm not a
> unix guru.(as you can see ;-)
> Thx

> George

OK, we've established you don't have attention deficit disorder, if you made
it through my last post.  Now, let's test your powers of patience ;)

I'm not sure the tr command would be any easier.  Besides, understanding
quoting is essential to everything you'll do.

Take baby steps:

(1) Run just the echo command.  It should work.

echo '"he says "Hi!" Then he realizes ^what'"'"'s^ happening."'

bash sees 3 quoted parts:
    '"he says "Hi!" Then he realizes ^what'
        Single-quote string taken literally.
    "'"
        Dbl-quote string interpolated by bash..."$var" will
        be replaced with the value of shell variable var;
        filename expansion of "*" characters, etc. will happen.
        But the single-quote here isn't treated specially.
    's^ happening."'
        Single-quoted string taken literally.

bash looses the quotes, and appends the values together:
    "he says "Hi!" Then he realizes ^what's^ happening."

bash knows that this came from three adjoining quoted strings, and thus,
treats it as one parameter that is sent to the echo command.

(2) Run just the awk command, but with a simpler script.  Don't worry that
you haven't given it an input stream; it will take it from the console.
Let's use an easier script:

    awk '{print "just got: " $0}'

If you run this, just type in some stuff...
    >awk '{print "just got: " $0}'
    hi
    just got: hi
    there
    just got: there

bash sees one quoted part:
    '{print "just got: " $0}'
        Single-quoted string taken literally.

And this is fine.  But, let's learn by counter-example:  If you had used
double quotes,
    awk "{print "just got: " $0}"
bash would see it entirely differently:
    "{print "8
         Dbl-quoted, but there is nothing to expand.
    just
        Unquoted string, delimited by whitspace.
        It directly adjoins the "{print", so bash will glue it together.
    got:
        Unquoted string, delimited by whitspace.
    " $0}"
        Dbl-quoted, but on top of that, $0 is expanded.
        $0 is the first element of the command line that
        started the current process.  So if this were a line
        in a script, $0 would usually be the name of the
        script.  From the command line, it expands to the
        first element of the command line  that started
        my shell.  On my system, that's "-bash".

The unquoted whitespace around those unquoted strings is what bash uses to
divide the command line into parameters.  So, what bash sends to awk is
    param 1: {print just  (a bad program fragment; awk dies)
        (That space was in the quoted "{print ".)
    param 2: got: (awk says, "huh?")
    param 3:  -bash} (a weird-looking awk option.)
        (Note the leading space.)

Awk dies, because param 1 isn't a parsable program.

Now, special characters in a string can be escaped, usually by putting a
backslash in front of them.  Different programs use different conventions,
and even in bash, which defaults to a backslash, you can change the escaping
character.  But here, it's backslash.  So, to avoid the complete
gobbeldygook, let's escape those internal double quotes:
    awk "{print \"just got: \" $0}"

Now, it gets expanded still, but bash treats it as one parameter when it
sends it to awk, and awk will appropriately think it's a program.
    "{print \"just got: \"  -bash}"

If you run this, and type lines in, awk will say "0" every time.  This is
because you are running the program:
    {print "just got:" - bash}
The "-" implies numeric operands.  The string "just got:" evaluates to 0,
and the variable bash, which is not ever initialized, evaluates to 0.  And,
nothing from nothing leaves nothing.  But ya gotta have something, if....

So, back to the second half of your test command:
    awk '{gsub(/\\^/, \"'\"'\"'\");print}'

bash sees some parameters to send to awk:
    '{gsub(/\\^/, \"'
        Pretty simple literal string.  Note there is no
        escaping in single-quoted strings, so \\^ passes
        to awk unchanged.
    \"
        Unquoted string, but adjoing the first, so bash
        will glue it together.  Since it is unquoted, bash
        looks for characters special to itself, and treats
        the backspash as an escape for the double-quote.
        So, only a double-quote will be gued on to the prior
        string.
    '\"'
        Pretty simple literal string, glued on to the two
        parameters before.
    \");print}'
        The big problem.  This string is unquoted; hence, bash
        also looks for characters that are special to itself.  The
        backslash escapes the double-quote, then bash
        continues to look at:
            ");print}'
        Note that the double-quote is now just a literal character,
        it's not going to tell bash a string is starting, because it
        was escaped.  Further into the string, it comes across a
        shell-level paren, important to bash, followed by a semicolon,
        also important to bash.  But bash isn't expecting either of
        these, so bash dies, with the error you got.

The reason bash errors out is that you were passing the string that was
inside the Perl string, to bash, without Perl doing its thing.  Literally,
this was

    "awk '{gsub(/\\^/,\"'\"'\"'\");print}'"

But, if the Perl compiler had sent this to its system() function, it would
have stripped out the escaped sequences before sending to the O/S:
    \\ becomes \
    \" becomes "
So, the value Perl was passing to system() was:
    awk '{gsub(/\^/,"'"'"'");print}'

This is what you would type in to the bash command line, too.  Its parts, to
review, are:

    '{gsub(/\^/,"'
        (BTW, there is no escaping in single-quoted strings,
        so \^ passes to awk unchanged.)
    "'"
    '");print}'

And the values of each string are all glued together, since the strings
adjoin each other on the command line.  So, the program parameter that awk
sees is

    {gsub(/\^/,"'");print}

Just take this stuff one step at a time, thinking, "If it has to run at
level n, how do I need to quote it in level n+1".  Start at the bottom.
And, when a test case dies, simplify it until there's just one element to
get right.  Then add the complexity back in.

    - Dan



Fri, 25 Mar 2005 11:20:12 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:
> Dan's "fully-quoted/escaped" version was intended for use inside a
> perl/awk script.  At the command line prompt what you need is:

> $ echo '"he says "Hi!" Then he realizes
> ^what'"'"'s^ happening."' | awk '{gsub(/\^/, "'"'"'");print}'
> "he says "Hi!" Then he realizes
> 'what's' happening."

> You should get the result in the last two lines if the quoting's OK.

Thanks Peter but I had tried all those combinations and followed Dan's
msg pretty thoroughly.
I tried the following again and this is what I got:

{site0}gchapman(97)$ echo '"he says "Hi!" Then he realizes
^what'"'"'s^ happening."' | awk '{gsub(/\^/, "'"'"'");print}'
awk: syntax error near line 1
awk: illegal statement near line 1

-----------------------------------------------

Did you try this? If so, maybe it's my bash somehow???



Fri, 25 Mar 2005 20:49:02 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:
> > Dan's "fully-quoted/escaped" version was intended for use inside a
> > perl/awk script.  At the command line prompt what you need is:

> > $ echo '"he says "Hi!" Then he realizes
> > ^what'"'"'s^ happening."' | awk '{gsub(/\^/, "'"'"'");print}'
> > "he says "Hi!" Then he realizes
> > 'what's' happening."

> > You should get the result in the last two lines if the quoting's OK.

> Thanks Peter but I had tried all those combinations and followed Dan's
> msg pretty thoroughly.
> I tried the following again and this is what I got:

> {site0}gchapman(97)$ echo '"he says "Hi!" Then he realizes
> ^what'"'"'s^ happening."' | awk '{gsub(/\^/, "'"'"'");print}'
> awk: syntax error near line 1
> awk: illegal statement near line 1

> -----------------------------------------------

> Did you try this? If so, maybe it's my bash somehow???

Yes, tried it with bash2.05b under Cygwin on a Winm98SE box.  From the
error message it looks as if you may be using a very early version of
awk that doesn't support gsub(). I'm guessing that you're using /bin/awk
on a Sun Solaris/SunOS box.  If you are then try using /usr/xpg4/bin/awk
instead as this is POSIX compliant.

HTH
Peter
--
Peter S Tillier  peter{dot}tillier<at>btinternet[dot]com
To email me direct please use the above address
This post represents the views of the author and does not
necessarily accurately represent the views of BT



Fri, 25 Mar 2005 21:46:09 GMT  
 Substituting single quotes from Perl via shell using awk and gsub


Quote:


> > > Dan's "fully-quoted/escaped" version was intended for use inside a
> > > perl/awk script.  At the command line prompt what you need is:

> > > $ echo '"he says "Hi!" Then he realizes
> > > ^what'"'"'s^ happening."' | awk '{gsub(/\^/, "'"'"'");print}'
> > > "he says "Hi!" Then he realizes
> > > 'what's' happening."

> > > You should get the result in the last two lines if the quoting's OK.

> > Thanks Peter but I had tried all those combinations and followed Dan's
> > msg pretty thoroughly.
> > I tried the following again and this is what I got:

> > {site0}gchapman(97)$ echo '"he says "Hi!" Then he realizes
> > ^what'"'"'s^ happening."' | awk '{gsub(/\^/, "'"'"'");print}'
> > awk: syntax error near line 1
> > awk: illegal statement near line 1

> > -----------------------------------------------

> > Did you try this? If so, maybe it's my bash somehow???

> Yes, tried it with bash2.05b under Cygwin on a Winm98SE box.  From the
> error message it looks as if you may be using a very early version of
> awk that doesn't support gsub(). I'm guessing that you're using /bin/awk
> on a Sun Solaris/SunOS box.  If you are then try using /usr/xpg4/bin/awk
> instead as this is POSIX compliant.

> HTH
> Peter
> --
> Peter S Tillier  peter{dot}tillier<at>btinternet[dot]com
> To email me direct please use the above address
> This post represents the views of the author and does not
> necessarily accurately represent the views of BT

Note that the error is now coming from awk, not from bash.  This is a good
indicator that your shell quoting is correct.

To see if your awk is behaving correctly, create the program in a file,
testawk.awk

testawk.awk
----------
    {gsub(/\^/,"'");print}

And from the command-line, run

    awk -f testawk.awk

and feed it data to make sure it is converting "^" to "'".

To see if your quoting is at issue, enter this script. testsh.sh

testsh.sh
----------
    echo 'param 0: "'"$0"'"'
    echo 'param 1: "'"$0"'"'
    echo 'param 2: "'"$0"'"'
    echo 'param 3: "'"$0"'"'

(Note the $0 is in double-quotes.  It probably doesn't need to be that way.)
Then, make it executable and run it:

    chmod +755 testsh.sh
    ./testsh.sh awk '{gsub(/\^/, "'"'"'");print}'

and make sure the output is something like this

    param 0: "./testsh.sh"
    param 1: "awk"
    param 2: "{gsub(/\^/, "'");print}"
    param 3: ""

- Dan



Fri, 25 Mar 2005 22:40:17 GMT  
 Substituting single quotes from Perl via shell using awk and gsub

Quote:


[...]

> > > {site0}gchapman(97)$ echo '"he says "Hi!" Then he realizes
> > > ^what'"'"'s^ happening."' | awk '{gsub(/\^/, "'"'"'");print}'
> > > awk: syntax error near line 1
> > > awk: illegal statement near line 1

> > > -----------------------------------------------

> > > Did you try this? If so, maybe it's my bash somehow???

> > Yes, tried it with bash2.05b under Cygwin on a Winm98SE box.  From
the
> > error message it looks as if you may be using a very early version
of
> > awk that doesn't support gsub(). I'm guessing that you're using
/bin/awk
> > on a Sun Solaris/SunOS box.  If you are then try using
/usr/xpg4/bin/awk
> > instead as this is POSIX compliant.

> > HTH
> > Peter
[...]

> Note that the error is now coming from awk, not from bash.  This is a
good
> indicator that your shell quoting is correct.

Yes and it's a typical SunOS /bin/awk error message.  It doesn't support
gsub().

[...]

--
Peter S Tillier
"Who needs perl when you can write dc and sokoban in sed?"
peter{dot}tillier<at>btinternet[dot]com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Sat, 26 Mar 2005 01:25:31 GMT  
 
 [ 26 post ]  Go to page: [1] [2]

 Relevant Pages 

1. gsub won't substitute single quotes

2. using gsub on a string containing single quotes

3. substituting make, Perl, shell with Smalltalk

4. need help substituting shell variable into search string with awk

5. awk not printing single quotes

6. printing single quote (apostrophe) with awk

7. Getting awk (or sed or anything else) to put single quotes into file

8. printing a single quote in awk

9. Quoting Swamp: Saving an awk command in a shell variable

10. REGULAR EXPRESSIONS; PERL/AWK/SED; SINGLE QUOTES/DOUBLE QUOTES

11. Return value from awk snipet via korn shell subroutine

12. replace single quote to double quote with an example

 

 
Powered by phpBB® Forum Software