Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-04-2008, 10:53 PM   #1
LQ Newbie
Registered: May 2008
Posts: 1

Rep: Reputation: 0 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....
Old 05-05-2008, 07:49 AM   #2
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
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.

Last edited by Telemachos; 05-05-2008 at 07:51 AM.
Old 05-05-2008, 08:34 AM   #3
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,630

Rep: Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393Reputation: 1393
First post ...
... urgent ...

Homework ???.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
sed replace string octeto Programming 4 06-06-2007 03:09 AM
how to replace string by other one in a file & without perl ? Xeratul Programming 4 05-26-2007 12:13 AM
How can I replace this string with another using sed? dave4545 Programming 7 01-27-2006 11:58 AM
Perl : Troubles to replace a string. philipina Programming 4 07-26-2004 06:09 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM

All times are GMT -5. The time now is 07:41 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration