2013-07-01 128 views
0

我有一個文件如下,我想搜索此行test name=""並刪除接下來的4行,包括第一個test name=""如何刪除perl中的特定行?

<test name="FT S_CREATE_DELETE_JOB"> 
<class name="11 .98. FT S_CREATE_DELETE_JOB"> 
COMPLETED 
<test name=""> 
<class name="11.98."> 
</class> 
</test> 
+0

什麼是「這條線」。此外,perl有很多方法可以做到這一點,試圖谷歌還沒找到代碼片段? – steabert

+7

你有沒有考慮過使用XML解析器? – TLP

+0

this line-> user1954970

回答

2

在大多數語言中,您不會直接操縱文件。相反,你打開第二個文件,還有寫你的結果,然後重命名第二個文件第一,刪除原:

use strict; 
use warnings; 
use autodie; 

open my $input_fh, "<", "input.txt"; 
open my $output_fh, ">", "output.txt"; 
while (my $line = <$input_fh>) { 
    chomp; 
    last if $line eq '<test name="">'; 
    print {$output_fh} "$line\n"; 
} 
close $input_fh; 
close $output_fh; 
unlink "input.txt"; 
rename "output.txt", "input.txt"; 

這是很基本的東西,所以我懷疑你不知道的Perl。也許是時候到learn it