2013-06-21 70 views
-1

我有一個文件看起來像這樣如何解析和檢測perl中日誌文件中的一行代碼塊?

[ 
MsgName:XYZ 
Status:Pass 
] 

[ 
MsgName:ABC 
Status:Pass 
] 

[ 
MsgName:bbb 
Status:Fail 
Error 
] 

[ 
MsgName:ttt 
Status:Pass 
] 

現在我想在這

讀取該文件,並創建2個日誌文件 包含所有數據,並含有唯一的錯誤數據的其他錯誤日誌中一個常規LOG

所以在這種情況下,錯誤日誌應該只顯示

[ 

MsgName:bbb 

Status:Fail 

Error 

] 

和regfular記錄所有

任何人都可以指導我如何做到這一點

我試過這樣

while($line[i]=<FileHandle> 
{ 

if($line[i]=~ /^s*\]/) 
{ 
    print OUT "recd new msg-->:/n $line[i]; 
    next; 
} 
do 

{ 
    print OUT "$line[i]"; 
    if($line[i]=~ /Error/) 

{ ####this section i cannot figure out hw to frame it get my desired output 

    print "error found 

    print ERROROUT ..... 

} until($line[i]=~ /^s\] 

這裏我用OUT的常規日誌輸出 和ERROROUT錯誤日誌

+0

我想補充我希望錯誤味精的整個塊,而不僅僅是錯誤行 – user2486369

回答

0

最簡單的竅門是存儲整個阻止你感興趣的事情,在你遍歷文件時用fifo。

#!/usr/bin/perl 

use strict; 
use warnings; 

my @fifo=('') x 5; # Initialize an empty array with size = 5 (Message Block Size) 
open(FILE,"log.txt"); 

while(<FILE>) 
{ 
    push(@fifo,$_);  # Add element to the end of array making its size 6 
    shift @fifo;   # Remove first element reverting its size back to 5 
    if($fifo[3]=~/Error/) # Check if 4th line of block has error in it 
    { 
     print @fifo; 
    } 
} 
close(FILE) 
+0

任何其他方式,而不是存儲和檢查....你有什麼辦法可以同時檢查,同時travsering – user2486369

+0

此代碼沒有按如果這是你所要求的,則不存儲整個文件。它只在遍歷文件時記住最後五行。所以本質上它是在遍歷時進行檢查。 – Jean

+0

Thnaks它我修改了它有點可能,它的工作。 – user2486369