Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace script

Posted on 2004-04-09
6
Medium Priority
?
1,106 Views
Last Modified: 2006-11-17
How i do a script that replaces determinate word of determinate file for other word? for example:
file.txt : expert-exchange.com cool!
/perl script.pl expert-exchange.com great!
file.txt : expert-exchange.com great!

tbkz.
0
Comment
Question by:cels9
  • 3
  • 3
6 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 10795352
#!/usr/bin/perl
$replace=pop;
$find=pop;
{local $^I=".bak"; local @ARGV=qw(file.txt);
 while( <> ){
    s/$find \S+/$find $replace/g;
     print;
 }
}
0
 
LVL 85

Expert Comment

by:ozo
ID: 10795359
Unless you meant to call
/perl script.pl file.txt cool! great!
in which case you might use
 #!/usr/bin/perl
$replace=pop;
$find=pop;
{local $^I=".bak";
 while( <> ){
    s/\b$find\b/$replace/g;
     print;
 }
}
0
 

Author Comment

by:cels9
ID: 10795766
the 1st or the 2nd? tnx
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 10796281
When the first is called as
perl script.pl expert-exchange.com great!
It replaces the word after "expert-exchange.com" with "great!" in file.txt

when the second is called as
perl script.pl file,txt cool! great!
it replaces the word "cool!" with "great!" in "file.txt"
 
Is either of those close to what you wanted?
0
 

Author Comment

by:cels9
ID: 10797550
ozo ure the one :P u request all questions... good!
0
 

Author Comment

by:cels9
ID: 10797568
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question