0

我正在编写一个脚本,该脚本对与特定文件类型匹配的每个文件进行模式替换。我正在使用这样的模式替换

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 代码片段:-

$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:
4

1 回答 1

2

$modifiedline = $_应该在循环之外完成。您正在破坏早期的修改。

于 2013-09-16T14:33:37.487 回答