-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