rjoost has asked for the wisdom of the Perl Monks concerning the following question:
Hello,
Between pattern1 and pattern2, I'd like to do a substitution.
I got it to work so that it prints to change to stdout, but it won't do the in-place edit of the file.
Here is my script so far:
#!/usr/bin/perl #print "ARGV:", $#ARGV; if ($#ARGV == 2 ) { my $host = $ARGV[0]; my $fn = $ARGV[1]; my $value = $ARGV[2]; #print "INPUTS: host:$host, fn:$fn, value:$value\n\n"; $systemsconfig = '/usr/local/etc/system.ini'; system("cp $systemsconfig /tmp/reconfig"); open CONFIG, "+< $systemsconfig" or die "Couldn't open $systemsconfi +g"; $^I = ".bak"; while (<CONFIG>) { if ( $_ =~ m/^\[$host\]/ .. m/#END/ ) { $_ =~ s/$fn=(.*)/$fn=$value/; print; # i thought print CONFIG here??? } } close CONFIG; }
Here is an example of the file that needs to be edited:
[bdstest] MainFunction= SystemType=U5 Env= Location= Serial=FW0094810719 Hostid=80c0dc14 OS=Solaris Version=8 Disk=1x8 Memory=128 CPU=1x360 qfe= SCSI= GraphACC= SupportContract= #END [custard] MainFunction= SystemType=U5 Env= Location= Serial=FW00250126 Hostid=80c0aee2 OS=Solaris Version=8 Disk=1x8 Memory=256 CPU=1x360 qfe= SCSI= GraphACC= SupportContract= #END
EXAMPLE: I'd like to edit the MainFuntion= line between bdstest and #END
So I call the script like this:
/usr/local/etc/test.pl bdstest MainFunction oracleThe system.ini file is in that format because this function will become part of a perl mod I'm slowly building to use it to integrate with some bash scripts to allow people to check out configuration information (I use Config::IniFiles)
Thanks for any help!
Rob
20031126 Edit by Corion: Added formatting
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: In-place editing in a script
by eric256 (Parson) on Nov 26, 2003 at 20:09 UTC | |
by rjoost (Novice) on Nov 26, 2003 at 20:48 UTC | |
by Roger (Parson) on Nov 27, 2003 at 00:19 UTC |