2013-11-01 74 views
0

我有一個名爲「boot.log」的文件。我模式匹配此文件,對某些關鍵字進行更改,然後將它們寫入名爲「bootlog.out」的文件。我不知道如何計算所做更改的數量並將其打印到「bootlog.out」。我很確定我需要使用foreeach循環和計數器,但我不確定在哪裏。以及如何打印所做的更改。這是我到目前爲止...計算在Perl中對文件所做的更改

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: $!"; 
+3

您的問題是什麼?你解決了什麼問題?你爲什麼刪除你的問題?如果你自己修復它,最好發佈一個答案並接受它。如果谷歌把我帶到這裏,我該讀什麼? – jkshah

回答

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"; 
相關問題