search and replace with $ and a /$ in the string

search and replace with $ and a /$ in the string

am 12.12.2005 19:41:13 von lbeckm3

Ok, bangin my head against a wall here, hope someone can help. I have
a script that does some smbclient copies to various servers and
recently had to change the password to one that includes a special
character ($). Now perl interprets that as the beginning of a variable
so it gets dropped and it doesn't work. I want to change this;

mypass$

to

mypass\$

that way perl leaves it alone. I've tried various combos of this;

perl -pi -e s/mypass\\$/mypass\\\\\$/g thefile

double escaping both the $ and the \ but nothing seems to work. The
search side seems alright as if I do this;

perl -pi -e s/mypass\\$/monkey/g thefile

all the mypass$ get replaced with monkey as expected, just the replace
side is biting me I believe. Any ideas? Thanks in advance!!

-Lyle

Re: search and replace with $ and a /$ in the string

am 12.12.2005 19:56:48 von Paul Lalli

lbec...@hotmail.com wrote:
> Ok, bangin my head against a wall here, hope someone can help. I have
> a script that does some smbclient copies to various servers and
> recently had to change the password to one that includes a special
> character ($). Now perl interprets that as the beginning of a variable
> so it gets dropped and it doesn't work.

Show the code that has this faulty interpretation.

> I want to change this;
> mypass$
> to
> mypass\$
>
> that way perl leaves it alone.

I strongly believe that is the wrong solution to the problem. If you
don't want Perl to interpret $foo as the scalar variable foo, simply
enclose it in single quotes within the source code. If the string is
being read from an external source (a file or user input), then Perl
will not be interpreting it incorrectly to begin with, and you have
misdiagnosed your problem.

I've tried various combos of this;
>
> perl -pi -e s/mypass\\$/mypass\\\\\$/g thefile
>
> double escaping both the $ and the \ but nothing seems to work. The
> search side seems alright as if I do this;
>
> perl -pi -e s/mypass\\$/monkey/g thefile
>
> all the mypass$ get replaced with monkey as expected, just the replace
> side is biting me I believe. Any ideas? Thanks in advance!!

First, what kind of quotes are you using around your -e code? You
didn't show any, which I believe is not valid. If you're using double,
there's a good chance you're running into shell interpolation problems,
not Perl. If you're using single, you should only need:

perl -pi -e's/mypass\$/mypass\\\$/g' the file

The first slash is to escape the $ in the pattern match. The second
two \\ are to create a single \ in the double-quotish replacement. The
final \ is to escape the $ in the double-quotish replacement.

Regardless of all that however, I still believe this is not the correct
solution. Perhaps you could post a short-but-complete example of the
code that isn't working the way you think it should?

Paul Lalli

Re: search and replace with $ and a /$ in the string

am 12.12.2005 20:08:44 von lbeckm3

The singe quotes around the string in the perl one-liner did it!
Exactly what I needed. The reason I didn't use the single 's in my
source code is that I do have other variables in there that I do want
interpreted. Here is a sample smbclient line in my file;

system("/usr/bin/smbclient //myntserver/myntshare mypass\$ -U myuser -W
mydomain -c 'cd thedirectory; put $oldname $newname'");

So I do want the $oldname and $newname perl variables properly
substituted. Thanks for the speedy response.

Re: search and replace with $ and a /$ in the string

am 12.12.2005 20:22:21 von Paul Lalli

lbeckm3@hotmail.com wrote:
> The singe quotes around the string in the perl one-liner did it!
> Exactly what I needed. The reason I didn't use the single 's in my
> source code is that I do have other variables in there that I do want
> interpreted. Here is a sample smbclient line in my file;
>
> system("/usr/bin/smbclient //myntserver/myntshare mypass\$ -U myuser -W
> mydomain -c 'cd thedirectory; put $oldname $newname'");
>
> So I do want the $oldname and $newname perl variables properly
> substituted. Thanks for the speedy response.

Ah, now see if you had shown this in the first place, I would have
given you a completely different response.

Don't let the shell interpret your string. Use the multi-arg form of
system()

system('/usr/bin/smbclient', '//myntserver/myntshare', 'mypass$', '-U',
'myuser', '-W', 'mydomain', '-c', "cd thedirectory; put $oldname
$newname");

for more information about the differences of the two forms of system()
calls, see:
perldoc -f system

Paul Lalli

Re: search and replace with $ and a /$ in the string

am 12.12.2005 20:32:44 von lbeckm3

Ahh cool. I didn't know about the multi-arg system call. I'll look
into using that in the future. Right now I have 100+ files to make
this change too, so that will have to come later. Again your help is
much appreciated. Thanks Paul!

-Lyle