2017-10-20 48 views
-1

我有一個程序,將提取一個名爲「output.zip」的zip文件併爲其創建一個目錄。我想創建一個日誌文件,如果某個單詞在我提取的任何文件中,在這種情況下單詞是Error。我收到的錯誤是說該文件不存在。我怎樣才能解決這個問題?如何使你剛剛提取的zip文件的輸出日誌文件

#!/usr/bin/perl 

use strict; 
use warnings; 

use Archive::Zip qw(:ERROR_CODES :CONSTANTS); 

my $sSource = "/home/glork/output.zip"; 
my $sDest = "/home/glork/zipped"; 



x_unzip($sSource,$sDest); 

sub x_unzip { 
my ($zip_file, $out_file, $filter) = @_; 
my $zip = Archive::Zip->new($zip_file); 
unless ($zip->extractTree($filter || '', $out_file) == AZ_OK) { 
    warn "unzip not successful: $!\n"; 
    } 
} 


open(LOGFILE, "/home/glork/zipped/var/log/*.log") or die "can't find file"; 

while(<LOGFILE>) { 
    print "Error in line $.\n" if(/ERROR/); 
} 
close LOGFILE; 
+1

不要嘗試在'open'中使用通配符。你不知道文件名嗎? – toolic

+0

@toolic是的我知道文件名,但它是一組文件 – Glorksnork

+0

您將不得不逐一打開每個文件。您無法同時寫入一堆文件。但是你可以有一個詞法文件句柄的數組,並遍歷它來寫入所有的文件。 – simbabque

回答

1

像這樣的東西應該工作。您提取所有文件,保存名稱。然後通過每個文件查找錯誤:

use strict; 
use warnings; 

use Archive::Zip qw(:ERROR_CODES :CONSTANTS); 

my $sSource = "/home/glork/output.zip"; 
my $sDest = "/home/glork/zipped"; 

my @extractedFiles; 
my $zip = Archive::Zip->new($sSource); 
foreach my $member ($zip->members) { 
    next if $member->isDirectory; 
    (my $extractName = $member->fileName) =~ s{.*/}{}; 
    $member->extractToFileNamed($sDest.'/'.$extractName); 
    push @extractedFiles, $extractName; 
    print "Extracted $sDest/$extractName\n"; 
} 

foreach my $logFile (@extractedFiles) { 
    open(LOGFILE, "$sDest/$logFile") or die "can't find file"; 
    while(<LOGFILE>) { 
     print "Error in line $.\n" if(/ERROR/); 
    } 
    close LOGFILE; 
} 
+0

很酷這個工程,但是,有沒有辦法讓我把結果放在一個單獨的文件 – Glorksnork

+0

只需打開一個文件,並將錯誤打印到該文件中。 – Andrey

相關問題