2011-02-23 26 views
-3

在Perl,perl的讀取多個文件

如何讀取多個文件一次,在月底

上午genrating報告,

每天一個日誌文件將創建,

我想讀整月的文件,

我的財產以後的文件這樣t.log.mmddyyy

+1

第一:它的「Perl的」帶有單環甚至爲了 - 如果加載列表到@ARGV,那麼你就可以處理所有的文件。這不是一個縮寫。其次你嘗試了什麼,你有什麼樣的數據?請閱讀關於如何提問的常見問題。 – Cfreak 2011-02-23 16:11:22

回答

1

glob函數將允許您檢索符合特定模式的文件名列表。

use strict; 
use warnings; 
use Getopt::Long; 

sub usage ($) { 
    my $msg = shift; 
    die <<"END_MSG"; 
*** $msg 
Usage: $0 --month=nn --year=nn PATH 

END_MSG 
} 

GetOptions('month=i' => \my $month, 'year=i' => \my $year); 

usage "Month not specified!" unless $month; 
usage "Year not specified!" unless $year; 
usage "Invalid month specified: $month" unless $month > 0 and $month < 13; 
usage "Invalid year specified: $year" unless $year > 0; 

my $directory_path = shift; 
die "'$directory_path' does not exist!" unless -d $directory_path; 

@ARGV = sort glob(sprintf("$directory_path/t.log.%02d??%02d", $month, $year)); 
while (<>) { # process all files 
    ... 
} 
0

你可以做一個

  • READDIR獲得目錄
  • 分析每個文件名以使文件列表確保它的每個文件相匹配的格式
  • 打開並閱讀
+0

但是dircotry包含整年,但我想閱讀permonth文件,完全一樣,月末,我會發送報告,這樣,我想一次讀取所有日誌文件 – Bharanikumar 2011-02-23 16:13:05