我正在开发一个脚本,它为每个匹配特定文件类型的文件执行模式替换。我正在使用像这样的模式替换
s/$modifydata[$loop]/$modifydata[$loop+1]/g;
但由于某种原因,这并没有取代所有的比赛。我在这里做错了,请帮忙。我已经通过几页进行模式替换,但仍然无法找到问题。对于失败的字符串我写了一个小测试perl,似乎工作我困惑: - (
输入: -
[Project:CISCO_GATEWAY]
network-feed=TIG_Generated:
stream-window-start=0
stream-window-end=450
test-config=tig_generated no_disk_check
预期产出: -
[Project:CISCO_GATEWAY_WB]
network-feed=WB_TIG_Generated:
stream-window-start2=0
stream-window-end=450
test-config=tig_generated no_disk_check
实际输出: -
[Fusion-Project:BSKYB_GATEWAY_WB]
network-feed=TIG_Generated:
stream-window-start=0
stream-window-end=450
test-config=tig_generated no_disk_check
Perl Code代码段: -
$modifydata[0]="TIG_";
$modifydata[1]="WB_TIG_";
$modifydata[2]="stream-window-start";
$modifydata[3]="stream-window-start2";
$modifydata[4]="CISCO_GATEWAY";
$modifydata[5]="CISCO_GATEWAY_WB";
open my $in, '<', $filteredfile or die "Can't read old file: $!";
open my $out, '>', "$filteredfile.tmp" or die "Can't write new file: $!";
while( <$in> )
{
for (my $loop=0; $loop < scalar(@modifydata); $loop=$loop+2)
{
($modifiedline = $_) =~ s/$modifydata[$loop]/$modifydata[$loop+1]/g;
print "1[$_] 2[$modifiedline] 3[$modifydata[$loop]] 4[$modifydata[$loop+1]]\n"
}
}
示例测试代码有效: - (
my $oldstring = "network-feed=TIG_Generated:";
(my $newstring = $oldstring) =~ s/TIG_/WB_TIG_/g;
print "$oldstring","\n";
print "$newstring","\n";
[rajeguna@ukstbuild3 bin]$ ./tmp.pl
network-feed=TIG_Generated:
network-feed=WB_TIG_Generated:
答案 0 :(得分:2)
$modifiedline = $_
应该在循环之外完成。你正在破坏早期的修改。