2012-11-06 3 views
-1

원격 디렉토리에있는 일부 로그 파일에서 문자열 패턴을 검색하려고합니다. 결과적으로 파일 이름, 행 번호/문자열의 발생을 원합니다. 파일 경로, 서버 주소, 자격 증명 및 검색 할 문자열 배열이 있습니다. here에서 아파치 로그 파서에 대한 몇 가지 코드가 있습니다. 내 구문 분석 코드를 실행하기위한 에이전트로 원격 컴퓨터에 아무 것도 설치하지 않고 파일을 구문 분석 할 수있는 방법이 있습니까?자바를 사용하여 원격 디렉토리의 서버 로그 파일에서 패턴을 검색하십시오.

+1

스택 오버 플로우에 오신 것을 환영합니다! [귀하의 질문을 연구하십시오] (http://stackoverflow.com/questions/how-to-ask). [이미 시도한 것] (http://whathaveyoutried.com/)이 있으면 질문에 추가하십시오. 아니라면 질문을 먼저 연구하고 시도한 다음 다시 방문하십시오. –

+0

우리는 그 사실을 알 수 없었습니다. – Woot4Moo

답변

1

원하는 것은 grep 프로그램을 "모방"하는 것입니다. "인터넷 검색"나는 오라클 예제 중 하나에서 this 예제를 찾았습니다. 이 클래스의 목적은 다음과 같습니다.

주어진 정규식 패턴과 일치하는 행의 파일 목록을 검색합니다.

그러나 알다시피 1.4.2 버전의 Java에서 제공되며 사용자가 직접 업데이트해야 할 수도 있습니다. 당신이 사용할 수있는 디렉토리에있는 모든 파일을 grep으로 그것을 사용하기 위해

import java.io.*; 
import java.nio.*; 
import java.nio.channels.*; 
import java.nio.charset.*; 
import java.util.regex.*; 


public class Grep { 

    // Charset and decoder for ISO-8859-15 
    private static Charset charset = Charset.forName("ISO-8859-15"); 
    private static CharsetDecoder decoder = charset.newDecoder(); 

    // Pattern used to parse lines 
    private static Pattern linePattern = Pattern.compile(".*\r?\n"); 

    // The input pattern that we're looking for 
    private static Pattern pattern; 

    // Compile the pattern from the command line 
    // 
    private static void compile(String pat) { 

     try { 
      pattern = Pattern.compile(pat); 
     } catch (PatternSyntaxException x) { 
      System.err.println(x.getMessage()); 
      System.exit(1); 
     } 
    } 

     // Use the linePattern to break the given CharBuffer into lines, applying 
     // the input pattern to each line to see if we have a match 
     // 
     private static void grep(File f, CharBuffer cb) { 
     Matcher lm = linePattern.matcher(cb); // Line matcher 
     Matcher pm = null;   // Pattern matcher 
     int lines = 0; 
     while (lm.find()) { 
      lines++; 
      CharSequence cs = lm.group(); // The current line 
      if (pm == null) 
      pm = pattern.matcher(cs); 
      else 
      pm.reset(cs); 
      if (pm.find()) 
      System.out.print(f + ":" + lines + ":" + cs); 
      if (lm.end() == cb.limit()) 
      break; 
      } 
     } 

     // Search for occurrences of the input pattern in the given file 
     // 
     private static void grep(File f) throws IOException { 

     // Open the file and then get a channel from the stream 
     FileInputStream fis = new FileInputStream(f); 
     FileChannel fc = fis.getChannel(); 

     // Get the file's size and then map it into memory 
     int sz = (int)fc.size(); 
     MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); 

     // Decode the file into a char buffer 
     CharBuffer cb = decoder.decode(bb); 

     // Perform the search 
     grep(f, cb); 

     // Close the channel and the stream 
     fc.close(); 
     } 

: 내가 도움이 희망

public void listFilesInDirectory(File dir) { 
    File[] files = dir.listFiles(); 
    if (files != null) { 
     for (File f : files) { 
      if (f.isDirectory()) { 
      listFilesInDirectory(f); 
     } 
     else 
      Grep.grep(f); 
     } 
    } 
} 

여기에 클래스입니다. 건배

관련 문제