我需要一些幫助來替換perl中兩個標記之間的行。我有,我想兩個標記之間修改行的文件:在perl中修改兩個標記之間的行
some lines
some lines
tag1
ABC somelines
NOP
NOP
ABC somelines
NOP
NOP
ABC somelines
tag2
正如你所看到的,我有兩個標籤,標籤1和TAG2,基本上,我想與標籤1之間NOP更換ABC的所有實例和tag2。這是代碼的相關部分,但不能代替。任何人都可以請幫忙..?
my $fh;
my $cur_file = "file_name";
my @lines =();
open($fh, '<', "$cur_file") or die "Can't open the file for reading $!";
print "Before while\n";
while(<$fh>)
{
print "inside while\n";
my $line = $_;
if($line =~ /^tag1/)
{
print "inside range check\n";
$line = s/ABC/NOP/;
push(@lines, $line);
}
else
{
push(@lines, $line);
}
}
close($fh);
open ($fh, '>', "$cur_file") or die "Can't open file for wrinting\n";
print $fh @lines;
close($fh);