원격 디렉토리에있는 일부 로그 파일에서 문자열 패턴을 검색하려고합니다. 결과적으로 파일 이름, 행 번호/문자열의 발생을 원합니다. 파일 경로, 서버 주소, 자격 증명 및 검색 할 문자열 배열이 있습니다. here에서 아파치 로그 파서에 대한 몇 가지 코드가 있습니다. 내 구문 분석 코드를 실행하기위한 에이전트로 원격 컴퓨터에 아무 것도 설치하지 않고 파일을 구문 분석 할 수있는 방법이 있습니까?자바를 사용하여 원격 디렉토리의 서버 로그 파일에서 패턴을 검색하십시오.
-1
A
답변
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);
}
}
}
여기에 클래스입니다. 건배
관련 문제
- 1. 자바를 사용하는 파일에서 유니 코드 문자열을 검색하십시오.
- 2. VB.net을 수정하여 패턴을 검색하십시오.
- 3. 자바를 사용하여 원격 컴퓨터를 재부팅
- 4. iPhone : "documents"디렉토리의 내용을 검색하십시오.
- 5. 자바를 사용하여 원격 컴퓨터 파일을 다운로드하십시오.
- 6. json 파일에서 데이터를 검색하십시오.
- 7. 파일에서 문자열을 검색하십시오.
- 8. 어떻게 자바를 사용하여 원격 데스크톱을 달성하기 위해
- 9. 자바를 사용하여 바이너리 데이터 패턴을 포함하는 문자열을 바이트로 변환하려면 어떻게해야합니까?
- 10. 자바를 사용하여 텍스트 파일에서 데이터 행 읽기
- 11. 자바를 사용하여 파일에서 정수 유형을 찾는 방법
- 12. 자바를 사용하여 파일에서 데이터를 내보내는 방법
- 13. 자바를 사용하여 엑셀 파일에서 데이터베이스로 데이터 업로드하기
- 14. 여러 디렉토리의 파일에서 정보 검색
- 15. ADO.net을 사용하여 MySQL 서버 시간을 검색하십시오.
- 16. 원격 .dbf 파일에서 SQL 서버 업데이트
- 17. 바이너리 파일에서 패턴을 찾으십니까?
- 18. 와일드 카드를 사용하여 파일을 검색하십시오.
- 19. 데이터가있는 원격 MySQL에 패턴을 제공하기 위해
- 20. 자바를 사용하는 작은 http 서버?
- 21. 원격 MSMQ로 로그 보내기
- 22. 원격 로그 오프 시트릭스 xenappsession
- 23. 장고 사용자가 원격 인증을 사용하여 로그 아웃
- 24. 로그 파일에서 grep을 얻어 개수
- 25. 비디오 파일에서 축소판 그림을 주기적으로 검색하십시오.
- 26. stream_socket_client를 사용하여 동시에 두 개의 원격 파일을 검색하십시오.
- 27. 자바를 사용하여 서비스를 시작하는 동안 로그 파일을 작성하는 방법
- 28. sed를 사용하여 로그 파일에서 디렉토리 추출
- 29. 자바를 사용하여 XML을 읽으십시오.
- 30. MSSQL2005를 사용하여 원격 서버 IP에 연결하는 방법?
스택 오버 플로우에 오신 것을 환영합니다! [귀하의 질문을 연구하십시오] (http://stackoverflow.com/questions/how-to-ask). [이미 시도한 것] (http://whathaveyoutried.com/)이 있으면 질문에 추가하십시오. 아니라면 질문을 먼저 연구하고 시도한 다음 다시 방문하십시오. –
우리는 그 사실을 알 수 없었습니다. – Woot4Moo