LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   help...how to replace string using Perl (http://www.linuxquestions.org/questions/programming-9/help-how-to-replace-string-using-perl-639909/)

adrian99420 05-04-2008 09:53 PM

help...how to replace string using Perl
 
Hi,

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

Code:

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

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:
Code:

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 12:59 PM.