【发布时间】:2014-06-19 22:55:19
【问题描述】:
我需要一些帮助来替换 perl 中两个标记之间的行。我有一个文件,我想在其中修改两个标签之间的行:
some lines
some lines
tag1
ABC somelines
NOP
NOP
ABC somelines
NOP
NOP
ABC somelines
tag2
如您所见,我有两个标签,tag1 和 tag2,基本上,我想用 tag1 和 tag2 之间的 NOP 替换所有 ABC 实例。这是代码的相关部分,但不能替换。谁能帮忙..?
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);
【问题讨论】:
标签: perl