2015-02-06 40 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中受益。