Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: how to search and replace some text in a .conf file

by punch_card_don (Curate)
on May 18, 2009 at 18:14 UTC ( [id://764693]=note: print w/replies, xml ) Need Help??


in reply to how to search and replace some text in a .conf file

open inFILE, "svs.conf" or die $!; @lines = <inFILE>; close inFILE; open outFILE, ">svs.conf" or die $!; for $i (0 .. $#lines) { ($param, $value) = split(/\=/, $lines[$i]); if ($param eq 'be_address') { $value = $new_value; print outFILE $param." = ".$value; } else { print outFILE $lines[$i]; } } close outFILE;

Replies are listed 'Best First'.
Re^2: how to search and replace some text in a .conf file
by chromatic (Archbishop) on May 18, 2009 at 21:12 UTC

    Please don't suggest that people use clobbery global variables or insecure two-arg open. Something like this is cleaner, safer, and simpler (though I didn't test it, and it may benefit from the use of File::Copy instead of rename):

    my $newfile = "svs.conf.$$"; open my $in, '<', 'svs.conf' or die "Can't read svs.conf: $!"; open my $out, '>', $newfile or die "Can't write '$newfile': $!"; while (<$in>) { s/\bbe_address\b =/, $new_value; print {$out} $_; } close $out; rename( 'svs.conf', $newfile );
Re^2: how to search and replace some text in a .conf file
by koti688 (Sexton) on May 18, 2009 at 18:28 UTC
    i am trying with this
    $input = "svs.conf"; open IN, "+> $input" or die "Unable to open $input for writing, $!,sto +pped"; while ( <IN> ) { s/remote_be_address/192.168.106.60/g; } close IN;
    Still not getting the correct result. :(

      You should also output your text somewhere. Look at print, or, if you want to do an in-place change, potentially look at Tie::File.

      There were a few things in punch_card_don that really should have been tidied up before being presented as sample code to someone new to Perl.

      First off, always use strictures (use strict; use warnings; - see The strictures, according to Seuss).

      Second, always use the three parameter form of open. The intent is clearer and it's not subject to the security issues that the two parameter form is prone to.

      Always use lexical file handles (open my $in, '<', $input or die ...;).

      And avoid slurping files. Actually that one you sorted out for yourself - well done!

      Something to consider - editing a variable record length file in place doesn't work because you can't adjust the size of an individual record without rewriting all the records that follow it. A text file is generally a variable length record file type - each line is a record. About the only way around the problem is to make an edited copy of the original file, then replace the original with the new file by judicious file deletion and renaming.


      True laziness is hard work
      If I'm not mistaken, the +> operator in your OPEN opens the file for reading & writing, creating it if it doesn't yet exist, but truncating it if it already exists.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://764693]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-26 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found