-   Programming (
-   - to replace string using Perl (

adrian99420 05-04-2008 09:53 PM to replace string using Perl

I wish to create a .bat file with the following content:


Set new=bye
perl -pi -i.bak -e "s/hello/new/g" hello.txt

I wish to replace the string "hello" to "bye" of a file hello.txt. However, the string keep changing to "new", I already assign new=bye (line1), so how can I replace the string "hello" to "bye"?

Million thanks in advance...I need this in urgent....

Telemachos 05-05-2008 06:49 AM

Drop the first line (Set new=bye), and make the second one look like this:

perl -i.bak -pe 's/hello/bye/g' hello.txt
I don't know anything about batch files or what that pause command is meant to do, for what that is worth. But the problem with the file as you wrote it seems to be that the Perl one-liner says "substitute any example of 'hello' with 'new' (including multiple times on one line)." The Perl one-liner is completely distinct from the Set command (I would think), so it can't "see" that you have assigned 'new' the value 'bye'. In any case, you shouldn't need two lines.

syg00 05-05-2008 07:34 AM

First post ...
... urgent ...

Homework ???.

All times are GMT -5. The time now is 07:06 AM.