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 oracle

The 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


In reply to In-place editing in a script by rjoost

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.