嗨,我正在尝试用以下字符串替换文件 test.txt 中的字符串:
<g
id="g16526">
<g
<g
id="gnnnnn">
<g
并将它们变成
<g
id="gg1">
<g
...
<g
id="ggn">
<g
使用这个 perl 脚本
#!C:/Strawberry/perl
open(FILE, "<test.txt") || die "File not found";
my @lines = <FILE>;
close(FILE);
my $string = '<g
id=';
my $string2 = '<g
<g';
my $anything = ".*";
my $replace = 'gg';
my @newlines;
my $counter = 1;
foreach(@lines) {
$_ =~ s/\Qstring$anything\Q$string2/$string$replace$string2$counter/g;
$counter++;
push(@newlines,$_);
}
open(FILE, ">test.txt") || die "File not found";
print FILE @newlines;
close(FILE);
但它不起作用,任何建议表示赞赏