views:

237

answers:

2

I'm using perl on windows and am trying to do a one liner using perl to substitute a placeholder in a file using a windows variable that contains a dollar sign. Does anyone know what the correct usage is to make it work with the dollar sign. I've tried various ways and can't seem to get it to work.

For example, I have a properties file that has a token in it (!MYPASSWORD!) that I'm trying to replace like:

somevalue="!MYPASSWORD!"

I have a batch file that looks up a variable say called NEWPASSWORD that contains the password $abc12345$ and I want to use perl substitution to replace the value like the following. Note I may not always know where the $ signs are so I cant escape them. For example another password may be abc$124$563:

echo %NEWPASSWORD%   <-- this would contain $abc12345$
perl -p -i.bak -e "s/!MYPASSWORD!/%NEWPASSWORD%/g" a.properties

When its done I want a.properties to be :

somevalue="$abc12345$"

Thanks in advance

+11  A: 

Use ' as regexp delimeter symbol. It will disable all variable substitution:

perl -p -i.bak -e "s'!MYPASSWORD!'%NEWPASSWORD%'g" a.properties
Ivan Nevostruev
ivan - thanks a ton. thats what I needed.
Nice! . . . . .
mobrule
Nice DWIMing, Ivan. An alternative would be to not let the shell fill in the value of the environmental variable: `s/!MYPASSWORD!/$ENV{NEWPASSWORD}/g`
Michael Carman
+2  A: 

I presume you are getting password from user input. why not just do that in Perl without having to go through batch since you are already using Perl? Its easier. you can then use modules like Term::Inkey to mask password and stuff.

ghostdog74
Surely, you are joking? There is no way that Perl's error handling and flow control are up to the task. A heavy duty language like DOS batch is much more appropriate for tasks like this.
daotoad
Ok, all joking aside, using a more powerful language, like Perl, will make these sorts of scripts much easier to maintain and reuse.
daotoad
you must be kidding too. Perl is so much more powerful than batch. In terms of error handling in batch, have you ever seen better ones other than %errorlevel% ?
ghostdog74