Solved

Replace script

Posted on 2004-04-09
6
1,099 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 84

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 84

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 84

Accepted Solution

by:
ozo earned 500 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Any syntax error for this clone.plscript 6 150
Perl Awk Need Help 3 150
Awk Question 2 136
Insert Text into odbc.ini file 15 75
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…

752 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