2015-02-06 43 views
0

我有一個應用服務器的ALOG,我需要得到與「E」標誌或「W」的線,如果有一條線後,我需要得到它。如何解析perl中的特定行?

我揣摩劇本:提前

#!/usr/bin/perl 
use strict; 
use warnings; 

my $msg; 
my $line; 
my $line2; 
main(@ARGV); 

sub rec { 
    #$msg= $line; 
    print $line; 
    while ($line2 = <FH>) { 
     if ($line2 !~ m/^\[/){ 
      #$msg = $msg.$line2; 
      print $line2; 
     } else { 
      if($line2 =~ m/ E | W /) { rec(); } 
      last; 
     } 

    } 
    #print $msg; 
} 
sub main { 
    open(FH,'SystemOut_15.02.05_17.00.02.log') or die "Error openong file : $!"; 
    while ($line = <FH>) { 
     if($line =~ m/ E | W/and $line =~ m/^\[/){ 
       rec(); 
     } 
    } 
    close FH; 
    } 

感謝。

樣品日誌:

[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S 
QLState: null 
*********************************************************** Start Server ******************************* 
0000003a JDBCException O OK 
0000003a JDBCException O OK 
********************************************************** End Server ******************************* 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S 
QLState: null 
                org.hibernate.util.JDBCExceptionReporter 
                org.hibernate.util.JDBCExceptionReporter 
                org.hibernate.util.JDBCExceptionReporter 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S 
QLState: null 
                org.hibernate.util.JDBCExceptionReporter 
                org.hibernate.util.JDBCExceptionReporter 
                org.hibernate.util.JDBCExceptionReporter 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S 
QLState: null 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S 
QLState: null 
                org.hibernate.util.JDBCExceptionReporter 
                org.hibernate.util.JDBCExceptionReporter 
                org.hibernate.util.JDBCExceptionReporter 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException O OK 

什麼,我需要得到:

[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, S 
QLState: null 
[2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, SQLState: null 
                 org.hibernate.util.JDBCExceptionReporter 
                 org.hibernate.util.JDBCExceptionReporter 
                 org.hibernate.util.JDBCExceptionReporter 
    [2/5/15 14:55:18:025 UTC] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, SQLState: null 
                 org.hibernate.util.JDBCExceptionReporter 
                 org.hibernate.util.JDBCExceptionReporter 
                 org.hibernate.util.JDBCExceptionReporter 
    [2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, SQLState: null 
    [2/5/15 14:55:18:025 UTC] 0000003a JDBCException E org.hibernate.util.JDBCExceptionReporter logExceptions SQL Error: 17006, SQLState: null 
                 org.hibernate.util.JDBCExceptionReporter 
                 org.hibernate.util.JDBCExceptionReporter 
                 org.hibernate.util.JDBCExceptionReporter 

一個忽略啓動和停止之間的線強方法是使用觸發器由YSTH指示;其他(弱)方法:

open(my $fh, '<', 'test2.log') or die "Error opening file : $!"; 
my $match = 0; 
while (my $line = <$fh>) { 
    if ($line =~ /^\*+/){ 
     $match = 0; ## initialize match if line start with star 
    } 
    if ($line =~ /^\[/) { 
      $match = $line =~ m/ E | W /; 
    } 
    print $line if $match; 
} 
close $fh; 
+0

這行代表單行(並在找到它時停止查找)?你能解釋一下你的代碼是做什麼的嗎?它看起來比你想描述的要複雜得多 – ysth 2015-02-06 15:25:19

+0

你能提供一個輸入樣本和你期望提取的內容嗎? – Sobrique 2015-02-06 15:29:14

+0

@ ysth/@Sobrique:不,它會查找所有發生的模式 – 2015-02-06 16:45:10

回答

2

很簡單;你只需要保持跟蹤你是否是目前正處於一個匹配多記錄的一部分,並使用flipflop operator (scalar context ..)排除線的開始/結束服務器範圍:

open(my $fh, '<', 'SystemOut_15.02.05_17.00.02.log') or die "Error opening file : $!"; 
my $match = 0; 
while (my $line = <$fh>) { 
    unless ($line =~ /^\*+ Start Server \*+$/ .. $line =~ /^\*+ End Server \*+$/) { 
     if ($line =~ /^\[/) { 
      $match = $line =~ m/ E | W /; 
     } 
     print $line if $match; 
    } 
} 
close $fh; 
+0

嗨,感謝您的代碼,但它沒有考慮到 – 2015-02-10 15:09:38

+0

嗨,感謝代碼,但它沒有考慮,如果它開始於:'code' [2/5/15 14:55:18: 025 UTC] 0000003a JDBCException W org.hibernate.util.JDBCExceptionReporter logExceptions SQL錯誤:17006,S QLState:null ************************* **********************************啓動服務器************** ***************** 0000003a JDBCException O OK 0000003a JDBCException O OK [2/5/15 14:55:18:025 UTC] 0000003a JDBCException W org.hibernate。 UTIL。JDBCExceptionReporter logExceptions SQL錯誤:17006,S QLState:null 'code' – 2015-02-10 15:17:17

+0

我不確定你的意思;你可以把它放在你的樣品輸入中(如果它應該被包括在內,則輸出) – ysth 2015-02-10 15:34:28

1

你可能從閱讀中獲益好代碼示例由Watching LogsTail following web server上的可用POE :: Wheel :: FollowTail球員提供。

由於您的日誌文件包含多行記錄,因此您的任務並不像初始時那麼簡單。您需要創建一個模式來識別記錄的開始([2/5/15 14:55:18:025 UTC] 0000003a JDBCException O),我會測試一個DateTime字符串後跟十六進制數字和JDBCException括號內可以是一個安全的選擇,並且如果所有以空格開頭的行都繼續相同的記錄(這將需要測試)。

你應該只捕獲每條記錄併發送給另一個事件處理,或者甚至使用POE::Wheel::Run之類的東西發送每個要在子進程上處理的記錄,假設你需要同時擁有一個詞法分析器(日誌taillor)和語義分析器(解釋日誌記錄)。

我不知道多行filter,但您可能會從閱讀代碼POE::Filter::RecordBlock中受益。