替换多行字符串中的行

时间:2018-05-20 17:22:03

标签: regex perl weblogic

我将Oracle WebLogic config.xml文件读入字符串。我正在寻找更新其中的一系列内容。我已经验证我正在读取文件,设置行,并能够使用我正在寻找的参数更新正确的行,但我似乎无法更新原始字符串。

这是主循环:

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;
}

如上所述,结果如下:

#======================================================================
<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]]

我似乎无法“重新找到”源标记,正如其中一条评论的$行所示,试图用任何内容替换$ochunk

1 个答案:

答案 0 :(得分:1)

你以一种非常全面的方式解决这个问题,这就是为什么我无法理解你最长时间想要做的事情。您实际尝试做的事情是

  

arguments元素

中的现有文本后插入其他字符串

你需要一个替代品。如果XML中确实存在多个这样的元素,我将它保留为全局。我还没能测试它,但我知道它编译了

$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