2015-02-06 4 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

+0

을 원하는 당신의 설명보다 더 복잡 많이 보인다? – Sobrique

+0

@ ysth/@Sobrique : 아니, 그것은 각 경기 후 패턴 –

답변

2

매우 간단합니다.

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

안녕, 그것은 고려 –

+0

안녕의 코드에 대한 감사를하지 않지만 그것이로 시작되는 경우는 고려하지 않는 덕분에 코드를하지만'code' [2/5/15 14시 55분 18초 : 세계 협정시 02시 05분] 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' –

+0

무슨 뜻인지 모르겠다. 당신이 그것을 샘플 입력에 넣을 수 있습니까? (그리고 출력이 포함될 예정이라면?) – ysth

1

당신은 아마 좋은 읽기 혜택을 누릴 것입니다 : 당신은 당신이 일치하는 여러 레코드의 일부를 현재 여부를 추적하고, 라인의 시작/종료 서버 범위를 제외 할 flipflop operator (scalar context ..)를 사용할 필요가 코드 샘플은 Watching LogsTail following web server에서 POE :: Wheel :: FollowTail의 멋진 POE :: Wheel :: Roller가 제공합니다.

로그 파일에 여러 줄 레코드가 있으므로 처음에 예상했던 것처럼 작업이 간단하지 않습니다. 레코드의 시작을 식별하는 패턴을 생성해야합니다 ([2/5/15 14 : 55 : 18 : 025 UTC] 0000003a JDBCException O) DateTime 문자열 다음에 이오고 HEX 숫자 및 JDBCException 대괄호 안은 안전한 선택 일 수 있으며 공백으로 시작하는 모든 행이 동일한 레코드를 계속 사용하는 경우 (테스트가 필요함).

아마도 각 레코드를 캡처하여 다른 이벤트에서 처리하도록 보내거나 각 레코드를 POE::Wheel::Run과 같은 자식 프로세스에서 처리하도록 보내야합니다. 어휘 파서 (로그 테일 러)와 의미 론적 파서 (로그 레코드 해석).

filter 멀티 라인을 모르지만 POE::Filter::RecordBlock 코드를 읽는 것이 좋습니다.

관련 문제