2012-09-10 2 views
0

로그 파일에서 정보를 추출하고 싶습니다. 내가 사용하고있는 패턴은 node-name과 명령의 프롬프트입니다. 명령 출력에 대한 정보를 추출하여 비교하고 싶습니다. 다음과 같이 샘플 출력을 고려하십시오.Java를 사용하여 로그에서 특정 패턴 추출하기

NodeName > command1 

    this is the sample output 

    NodeName > command2 

    this is the sample output 

다음 코드를 시도했습니다.

public static void searchcommand(String strLineString) 
    { 


      String searchFor = "Nodename> command1"; 
      String endStr = "Nodename"; 
      String op=""; 
      int end=0; 
       int len = searchFor.length(); 
       int result = 0; 
       if (len > 0) { 
       int start = strLineString.indexOf(searchFor); 
       while(start!=-1){ 
     end = strLineString.indexOf(endStr,start+len); 

       if(end!=-1){ 
        op=strLineString.substring(start, end); 

       }else{ 
        op=strLineString.substring(start, strLineString.length()); 
       } 
       String[] arr = op.split("%%%%%%%"); 
       for (String z : arr) { 
        System.out.println(z); 
       } 

        start = strLineString.indexOf(searchFor,start+len); 


       } 

       } 



    } 

문제는 코드가 너무 느려 데이터를 추출 할 수 없다는 것입니다. 그렇게 할 수있는 다른 방법이 있습니까?

EDIT 1 위의 코드에서 문자열로 읽은 로그 파일입니다.

정규 표현식을 사용

+0

전체 로그를 문자열로 가지고 있습니까? –

+0

나는 파일을 위 코드의 문자열로 읽습니다. –

+0

그러한 문자열은 얼마나 큽니까? 시간이 걸리는 것을 측정 했습니까? 로그를 문자열로 읽어들입니까? 시작/정지 또는 분할 찾기? 입력 내용이 코드와 일치하지 않는 특정 구문 분석 최적화를 제공하는 것은 어렵습니다. –

답변

0

나의 제안 ..

public static void main(String[] args) { 
     String log = "NodeName > command1 \n" + "this is the sample output \n" 
       + "NodeName > command2 \n" + "this is the sample output"; 

     String lines[] = log.split("\\r?\\n"); 
     boolean record = false; 
     String statements = ""; 
     for (int j = 0; j < lines.length; j++) { 
      String line = lines[j];   
      if(line.startsWith("NodeName")){ 

       if(record){ 
        //process your statement 
        System.out.println(statements); 
       } 

       record = !record; 
       statements = ""; // Reset statement 
       continue; 
      } 

      if(record){    
       statements += line; 
      } 
     } 
    } 
+0

또는 코드를 최적화하려면 strLineString 변수에서 검색 한 문자열을 제거하면됩니다. 마지막 부분에서 start를 재 할당하면 strLineString = strLineString.subString (end); 시작 = 0; – bhatanant2

0

여기 내 제안이다. 여기에 하나입니다

final String input = " NodeName > command1\n" + 
      "\n" + 
      " this is the sample output1 \n" + 
      "\n" + 
      " NodeName > command2 \n" + 
      "\n" + 
      " this is the sample output2"; 

    final String regex = ".*?NodeName > command(\\d)(.*?)(?=NodeName|\\z)"; 

    final Matcher matcher = Pattern.compile(regex, Pattern.DOTALL).matcher(input); 

    while(matcher.find()) { 
     System.out.println(matcher.group(1)); 
     System.out.println(matcher.group(2).trim()); 
    } 

출력 : 그래서

1 
this is the sample output1 
2 
this is the sample output2 

, 정규식 분해합니다 : 그것은 최초의 "노드 이름> 명령"을 찾을 때까지

먼저, 모든 징후를 건너 뜁니다을 따라 번호로. 이 번호는 어떤 명령이 출력을 생성했는지 알고 싶어합니다. 다음으로 우리는 (lookahead를 사용하여) 다른 NodeName이나 입력의 끝을 찾을 때까지 다음의 모든 표시를 가져온다.

관련 문제