-1
<test name="">
<class name="48.">
</class>
</test>
上述项可以来到我的文件的四倍左右,我想从文件中删除这些条目,所以我使用以下逻辑:要从perl文件中删除多个数据块?
#!/usr/bin/perl
$count=0;
open (FILE, '/tmp/uday/testng-results.xml');
[email protected] = <FILE>;
for(my $i=0;$i<$#lines;$i++)
{
if($lines[$i] =~ /<test name="">/)
{
$count++;
}
}
print "$count\n";
while($count!=0)
{
open (FILE, '/tmp/uday/testng-results.xml');
my @lines = <FILE>;
for(my $i=0;$i$#lines;$i++)
{
if($lines[$i] =~ /<test name="">/)
{
print "Start line number $i\n";
$Start=$i;
$End=$i+4;
print "$End Line number End\n";
system("sed -i '$Start,$End d' testng-results.xml");
}
}
$count--;
}
是他们的任何更好的方式做它?
你应该使用缩进,它会让你的生活变得更轻松。 “严格使用”也是如此。使用警告;' – TLP
尝试'perl -ne'除非(m##.. m# #){print}'yourfile> newfile'。你真的应该使用一个解析器,但是这个快速入侵可能就足够了。 –
TLP
谢谢,但我不会写它到一个新的文件,我希望它在删除后在原始文件本身。 – user1954970