Solved

Replace script

Posted on 2004-04-09
6
1,097 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 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…

809 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