2017-11-30 1 views
0

요구 사항 : 텍스트 파일을 구문 분석하고 특정 파일의 패턴을 다른 파일로 추출해야합니다. 출력 파일에 날짜가있는 행과 오류가 필요합니다.Windows 배치 파일에서 정규식을 사용하여 특정 패턴을 찾는 방법은 무엇입니까?

배치 파일 코드 :

findstr /r "^\\d{4}-\\d{2}-\\d{2}\\s+\\d{2}:\\d{2}:\\d{2},\\d{3} \<ERROR.*" server.log > "%CD%"/test.txt 
pause 

입력 파일 : Sample.log

2017-11-28 00:40:16,791 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 269) WELD-000019: Error destroying an instance 
2017-11-28 00:40:16,791 INFO [stdout] (ServerService Thread Pool -- 269) [ERROR][faces context is null in context utils1] 
2017-11-28 00:40:16,791 INFO [stdout] (ServerService Thread Pool -- 269) [ERROR][faces context is null in context utils1] 
2017-11-28 00:40:16,791 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 269) WELD-000019: Error destroying an instance 
2017-11-28 00:40:16,791 INFO [stdout] (ServerService Thread Pool -- 269) [ERROR][faces context is null in context utils1] 
2017-11-28 00:40:16,791 INFO [stdout] (ServerService Thread Pool -- 269) [ERROR][faces context is null in context utils1] 

예상 출력 :

2017-11-28 00:40:16,791 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 269) WELD-000019: Error destroying an instance 
2017-11-28 00:40:16,791 ERROR [org.jboss.weld.Bean] (ServerService Thread Pool -- 269) WELD-000019: Error destroying an instance 

그러나 현재 코드가 출력 파일의 모든 내용을 반환 . 시도한 단어 경계 \ 너무. 정규식의 제한 구현 findstr 년대 주어진 filename1이 작업을 수행해야 당신의 sourcefilename을 포함

+1

'd {3} \ Stephan

+1

또한'\ d'가 없습니다. , '\ s' 및'+' – Stephan

+0

@Stephan 예 .with/c "ERROR"오류가있는 줄을 필터링 할 수 있습니다 .Eg) 2017-11-28 00 : 40 : 16,791 INFO [stdout] (ServerService 스레드 풀 -269) 일부 문자열에 ERROR가 있습니다. \ d, \ s, +, count가없는 경우에는 다른 방법으로 –

답변

3
FINDSTR /b /r "....-..-.....:..:..,....ERROR" "%filename1% 

.

+0

예 this works.can please please 이 논리 뒤에있는 논리는 ... –

+1

@johnsigeorge'/ b'는 문자열의 시작 부분과 일치하고,/r은 정규 표현식입니다. 그런 다음 오류가 발생할 때까지 4 자 및 대시, 2 자 및 대시 등을 매치하십시오. 문자열에 날짜가없는 문자열이 있어야하지만 여전히 오류가 있습니다. 일치하지 않습니다. –

+0

@GerhardBarnard 시간을 내 주셔서 감사합니다 .BTW 위의 솔루션을 잘 작동합니다. 알 수없는 시나리오와 더 나은 이해를 알려 주시기 바랍니다. –