Search code examples
regexperlweblogic

Replace lines in a multi-line string


I have an Oracle WebLogic config.xml file read into a string. I'm looking to update a series of lines in it. I've verified that I'm reading the file, getting the lines set, and able to update the correct line with the parameters I'm looking for, but I can't seem to update the original string.

Here's the main loop:

while ( $lines =~ m{(<arguments>.*?</arguments>)}mgs ) {

    my $nchunk = my $ochunk = $1;

    print "#" . '=' x 70 . "\n";

    my ($ms)  = $ochunk =~ m{.*/(.*?)\.out.*};
    my $nname = $monster->{$domain}->{$ms}->{nodeName};
    my $tname = $monster->{$domain}->{$ms}->{tierName};

    my $newentry = sprintf(" %s %s.nodeName=-Dappdynamics.agent.nodeName=%s",
            $appdjar, $ms, $nname);
    $newentry .= " $ms.appdynamics.tierName=-Dappdynamics.tierName=$tname";

    $nchunk =~ s/(<\/arguments>)/$newentry\1/g;

    print "$ochunk\n";
    print "#" . '-' x 70 . "\n";
    print "$nchunk\n";

    # $lines =~ s!$ochunk!!msg;
    # $lines =~ s!$ochunk!$nchunk!msg;
}

As written, that results in:

#======================================================================
<arguments>-Xms512m -Xmx512m -Dweblogic.system.BootIdentityFile=/opt/app/oracle/user_projects/domains/AccountingServices_Domain/boot.properties -Dweblogic.Stdout=/opt/app/oracle/user_projects/logs/AccountingServices_Domain/AccountingCommon_MS1.out -XX:+HeapDumpOnOutOfMemoryError  -XX:HeapDumpPath=/opt/app/oracle/user_projects/logs/AccountingServices_Domain/dumps  -XX:+UnlockCommercialFeatures  -XX:+FlightRecorder  -Dcom.sun.management.jmxremote.port=40124  -Dcom.sun.management.jmxremote.ssl=false  -Dcom.sun.management.jmxremote.authenticate=false  -XX:+UnlockCommercialFeatures -XX:+FlightRecorder  -XX:FlightRecorderOptions=defaultrecording=true,disk=true,repository=/opt/app/oracle/user_projects/logs/AccountingServices_Domain,maxage=10m,dumponexit=true,dumponexitpath=/opt/app/oracle/user_projects/logs/AccountingServices_Domain  -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -Dlog4j.configuration=file:/opt/app/oracle/user_projects/applications/AccountingServices_Domain/log4j.xml</arguments>
#----------------------------------------------------------------------
<arguments>-Xms512m -Xmx512m -Dweblogic.system.BootIdentityFile=/opt/app/oracle/user_projects/domains/AccountingServices_Domain/boot.properties -Dweblogic.Stdout=/opt/app/oracle/user_projects/logs/AccountingServices_Domain/AccountingCommon_MS1.out -XX:+HeapDumpOnOutOfMemoryError  -XX:HeapDumpPath=/opt/app/oracle/user_projects/logs/AccountingServices_Domain/dumps  -XX:+UnlockCommercialFeatures  -XX:+FlightRecorder  -Dcom.sun.management.jmxremote.port=40124  -Dcom.sun.management.jmxremote.ssl=false  -Dcom.sun.management.jmxremote.authenticate=false  -XX:+UnlockCommercialFeatures -XX:+FlightRecorder  -XX:FlightRecorderOptions=defaultrecording=true,disk=true,repository=/opt/app/oracle/user_projects/logs/AccountingServices_Domain,maxage=10m,dumponexit=true,dumponexitpath=/opt/app/oracle/user_projects/logs/AccountingServices_Domain  -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints -Dlog4j.configuration=file:/opt/app/oracle/user_projects/applications/AccountingServices_Domain/log4j.xml -javaagent:/opt/app/appdynamics/universal-agent/monitor/java/javaagent.jar AccountingCommon_MS1.nodeName=-Dappdynamics.agent.nodeName=AccountingCommon_2123 AccountingCommon_MS1.appdynamics.tierName=-Dappdynamics.tierName=AccountingCommon</arguments>
[[snip]]

I can't seem to 're-find' the source chunk as indicated by one of those commented $lines trying to replace $ochunk with nothing.


Solution

  • You're going about this in a very round-about way, which is why I couldn't fathom what you were trying to do for the longest time. What you're actually trying to do is

    Insert an additional string after the existing text in an arguments element

    And you need just a substitution. I've left it global in case there really are multiple such elements in the XML. I've not been able to test it, but I do know that it compiles

    $lines =~ s{ (<arguments>) (.*?) (</arguments>) }{
    
        my ($otag, $text, $ctag)  = ($1, $2, $3);
        my ($ms)   = $text =~ m{.*/(.*?)\.out};
    
        my $msdata = $monster->{$domain}{$ms};
        my $node   = $msdata->{nodeName};
        my $tier   = $msdata->{tierName};
    
        my $newentry = " $appdjar $ms.nodeName=-Dappdynamics.agent.nodeName=$node";
        $newentry   .= " $ms.appdynamics.tierName=-Dappdynamics.tierName=$tier";
    
        $otag . $text . $newentry . $ctag;
    }segx