0

我有一个名为“boot.log”的文件。我正在模式匹配这个文件,对某些关键字进行更改,然后将它们写入一个名为“bootlog.out”的文件。我不确定如何计算所做的更改数量并将它们打印到“bootlog.out”。我很确定我需要使用 foreeach 循环和计数器,但我不确定在哪里。以及如何打印所做的更改。这是我到目前为止所拥有的...

open (BOOTLOG, "boot.log") || die "Can't open file named boot.log: $!";
open (LOGOUT, ">bootlog.txt") || die "Can't create file named bootlog.out: $!\n";

while ($_ = <BOOTLOG>)
{
    print $_;
    s/weblog/backupweblog/gi;
    s/bootlog/backupbootlog/gi;
    s/dblog/DBLOG/g;
    print LOGOUT $_;
}

close (LOGOUT) || die "Can't close file named bootlog.txt: $!\n";
close (BOOTLOG) || die "Can't close the file named boot.log: $!";
4

1 回答 1

5

替换正则表达式返回所做的替换次数。以下是您的代码的更新副本作为示例:

open (my $bootlog, '<', "boot.log")   || die "Can't open file named boot.log: $!";
open (my $logout, '>', "bootlog.txt") || die "Can't create file named bootlog.out: $!\n";

my $count = 0;
while (<$bootlog>)
{
    print $_;
    $count += s/weblog/backupweblog/gi;
    $count += s/bootlog/backupbootlog/gi;
    $count += s/dblog/DBLOG/g;
    print {$logout} $_;
}

close ($logout)  || die "Can't close file named bootlog.txt: $!\n";
close ($bootlog) || die "Can't close the file named boot.log: $!";
print "Total items changed: $count\n";
于 2013-11-01T04:35:33.843 回答