Solved

Replace script

Posted on 2004-04-09
6
1,094 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
Comment Utility
#!/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
Comment Utility
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
Comment Utility
the 1st or the 2nd? tnx
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 84

Accepted Solution

by:
ozo earned 500 total points
Comment Utility
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
Comment Utility
ozo ure the one :P u request all questions... good!
0
 

Author Comment

by:cels9
Comment Utility
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl for loop for 2000 ms 7 91
Expect & sshpass package for Solaris 10 x86 15 850
Perl 101 11 68
combine multiple lines 2 21
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…
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 …
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now