Perl从日志中收集包含特定内容的xml片段
我从另一个stackoverflow问题复制了一个脚本,但它似乎替换了变量的内容,有人能告诉我错误吗?如果删除“>OOK的If检查,则目标是在包含特定模式时捕获整个Perl从日志中收集包含特定内容的xml片段,perl,search,Perl,Search,我从另一个stackoverflow问题复制了一个脚本,但它似乎替换了变量的内容,有人能告诉我错误吗?如果删除“>OOK的If检查,则目标是在包含特定模式时捕获整个标记(>OK=~m不修改变量,但返回true或false。$xml包含当前行。仅当它包含>OK样本数据时才会打印。因为这里的答案可能是不使用正则表达式进行xml解析。但如果没有数据,我无法为您提供更好的解决方案。@chorobae选中OK并保留print语句它将打印整个xml片段,否则它只打印包含OK的行,$xml变量在哪里修改?输入
标记(>OK=~m
不修改变量,但返回true或false。$xml
包含当前行。仅当它包含>OK样本数据时才会打印。因为这里的答案可能是不使用正则表达式进行xml解析。但如果没有数据,我无法为您提供更好的解决方案。@chorobae选中OK并保留print语句它将打印整个xml片段,否则它只打印包含OK的行,$xml变量在哪里修改?输入进入$\u
。然后在if
的范围内进行修改,因为s/
在默认情况下对$\u
起作用。所以这是任何人的猜测然后,$xml
会得到什么,分配给$\uu
。我们需要查看数据。
# Example usage:
# perl script.pl data.xml RootTag > RootTag.xml
use strict;
use warnings;
my $tag = pop;
while (<>){
if ( s/.*(<$tag>)/$1/ .. s/(<(\/)$tag>).*/$1/ ) {
my $xml = $_;
if ($xml =~ m/>OK</) {
print "$xml";
}
}
}
reioirioree
brebreberbre
rbebrbebre
<test>
<id>1</id>
<status>OK</status>
</test>
bbrtbtrbt
rtbtrb
<test>
<id>2</id>
<status>KO</status>
</test>
brtoibjtrbi
bebbetreb
<test>
<id>3</id>
<status>OK</status>
</test>
dfbreberbreb
berbrebre
<test>
<id>1</id>
<status>OK</status>
</test>
<test>
<id>3</id>
<status>OK</status>
</test>
use strict;
use warnings;
my $tag = pop;
my ($inside_tag, $found, @buff);
while (<>)
{
if (s/.*(<$tag>)/$1/) {
$inside_tag = 1;
}
elsif (s|(</$tag>).*|$1|) { #/
$inside_tag = 0;
if ($found) {
print @buff, $_;
$found = 0;
}
@buff = ();
}
next unless $inside_tag;
push @buff, $_;
$found = 1 if />OK</;
}
if (my $seq = /BEG/ .. /END/)
{
if ($seq == 1) { # first line of range
# ...
}
elsif ($seq =~ /EO$/) { # last line of range
# ...
}
else { ... } # inside