Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace part of a file

Status
Not open for further replies.

sun9

Programmer
Dec 13, 2006
31
US
How do I go about replacing a part of a file with new content say just one line with a new line?

Thanks.
 
I like Tie::File too. Here is another way, although this example is not replacing an entire line, it could:

Code:
my $filename = 'file.txt';
my $find = 'bar';
my $replace = 'foo';
replace($find,$replace,$filename);

sub replace {
   local $^I   = '.bak';
   my $find    = quotemeta(shift) or return(0);   
   my $replace = shift or return(0);
   local @ARGV = shift or return(0);;
   while(<>){
      s/$find/$replace/g;
      print;
   }
   close;  
   return(1);
}


- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top