2016-10-28 1 views
-3

CR/LF로 인해 때로는 제대로 포맷되지 않은 파일이 있습니다.Windows 배치 파일을 사용한 데이터 정리를위한 Regex 패턴 및

R00023j Field1 Field2 .... CR/LF 
R00024n Field1 Field2 .... CR/LF 
R00025k Field1 Field2 .... CR/LF 

하지만 언젠가 나는 CR/LF 하나 개의 필드에 삽입하고는이 같은 파일을 생성 :

좋은 파일은 다음과 같습니다

R00023j Fiel CR/LF 
d1 Field2 .... CR/LF 
R00024n Field1 Field2 .... CR/LF 
R00025k Field1 Field2 .... CR/LF 

"상품"CR/LF (줄 끝 부분) 및 "불량"CR/LF (필드).

좋은 CR/LF가 바로 다음 라인에있는 RxxxxxY 인 것으로 간주 할 수 있습니다. 다른 모든 CR/LF는 불량이므로 . (점)으로 교체해야합니다.

x : 자리
Y : 편지

윈도우 배치 파일 및 정규식을 가진 파일의 데이터 정리를 달성 할 수 있습니다 어떻게?

+0

지금까지 해보신 것을 공유하십시오! – aschipfl

+0

필드 수는 일정합니까? 필드 separador 무엇입니까? 더 자세하게 설명하십시오. 이렇게하면 좋은 대답을주기가 복잡해집니다 ..... – SachaDee

답변

0

사양이 불완전합니다. CR/LF가 파일의 맨 끝에있는 경우에도 좋습니다.

JREPL.BAT - A regex find/replace utility을 사용하는 간단한 해결책이 있습니다. JREPL은 XP 이후의 모든 Windows 시스템에서 기본적으로 실행되는 순수한 스크립트 (하이브리드 배치/JScript)입니다. 전체 문서는 페이징 된 도움말의 경우 jrepl /? 또는 jrepl /??을 통해 명령 줄에서 사용할 수 있습니다.

명령 행에서 간단한 한 줄 짜기 만 있으면됩니다. 소스는 bad.txt, 당신은 다음, good.txt을 만들려면 :

jrepl "\r?\n(?=.)(?!R\d{5}[a-z])" "." /i /m /f bad.txt /o good.txt 

당신은 /o -을 통해 원본 파일을 덮어 쓸 수 있습니다 :

jrepl "\r?\n(?=.)(?!R\d{5}[a-z])" "." /i /m /f file.txt /o - 

사용 CALL JREPL을 명령을 넣을 경우 배치 스크립트 내에서.

줄을 모두 검색해야하므로 전체 파일을 메모리에로드하는 /M 옵션을 사용해야합니다. 이렇게하면 처리 할 수있는 파일 크기가 제한됩니다. 한도는 1 ~ 2GB 사이라고 생각합니다.

0
다음은 작동합니다

, 거기에 특수 문자가 파일에없는 당신이 당신의 요구에 적응해야 할 때, 몇 가지에주의하시기 바랍니다 RxxxxxY

@echo off 
setlocal enabledelayedexpansion 
for /f "delims=" %%a in (t.txt) do (
    echo %%a|findstr /b "R[0-9][0-9][0-9][0-9][0-9][a-z]">nul && (
    echo(!line! 
    set line=%%a 
) || (
    set line=!line!%%a 
) 
) 
echo %line% 

내부에 추가 CRLF가 발생 시킬수하지 않는 경우 findstr limitations

0

직접적인 노력을 보이지는 않았지만 필자는 당면 과제가 내게 상당히 어려워 보이기 때문에 스크립트를 제공하기로 결정했습니다. 그래서 여기에 우리가 (코드 설명 발언을 많이 contans, 너무 무서워하지 않음) 이동 :

검색 패턴 ( RxxxxxY)을 처리해야 경우
@echo off 
setlocal EnableExtensions DisableDelayedExpansion 

rem // Define constants here: 
rem /* Regular expression string for `findstr` command (to match `RxxxxxY`); 
rem do not state `[a-z]` expression due to a nasty flaw of `findstr`!: */ 
set "_SEARCH=R[0-9][0-9][0-9][0-9][0-9][abcdefghijklmnopqrstuvwxyz]" 
set "_REPLAC=." & rem // (character which each bad CR+LF is to be replaced by) 

rem // Enumerate all files provided by command line arguments: 
for %%F in (%*) do (
    rem /* Store paths of input and output files; to overwrite input files, 
    rem set `FILENEW` to `%%~fF` also: */ 
    set "FILEOLD=%%~fF" 
    set "FILENEW=%%~dpnF_NEW%%~xF" 
    rem // Initialise buffer for concatenated line strings: 
    set "LBUF=" 
    rem // Read currently iterated file line by line (ignoring empty lines): 
    setlocal EnableDelayedExpansion 
    for /F "delims=" %%L in ('type "!FILEOLD!" ^& ^> "!FILENEW!" rem/') do (
     endlocal 
     rem // Store current line string: 
     set "LINE=%%L" 
     setlocal EnableDelayedExpansion 
     rem/ Double " due to pipe: 
     set "LINE=!LINE:"=""!" 
     rem /* Loop iterating once only over the current line with quotation 
     rem marks doubled in order to avoid trouble with the pipe later; 
     rem this allows disabling delayed expansion which might cause 
     rem trouble with pipes too in case `!` or `^` characters appear: */ 
     for /F "delims=" %%K in (^""!LINE!"^") do (
      endlocal 
      rem /* Feed line string into `findstr` command using a pipe: 
      rem for case-insensitivity, add switch `/I` to `findstr`: */ 
      echo("%%~K"| > nul findstr /X /R /C:\"%_SEARCH%.*\" 
      rem // Test whether `findstr` encountered a match: 
      if ErrorLevel 1 (
       rem /* No match encountered, so CR+LF was bad, hence 
       rem concatenate previous buffer with current line, 
       rem separated by the predefined character; due to a 
       rem preceding `endlocal` command, `LINE` no longer 
       rem contains the doubled quotation marks at this point; 
       rem the `for /F` loop transfers the resulting string over 
       rem the `endlocal` barrier safely: */ 
       setlocal EnableDelayedExpansion 
       for /F "delims=" %%E in (^""!LBUF!%_REPLAC%!LINE!"^") do (
        endlocal 
        set "LBUF=%%~E" 
       ) 
      ) else (
       rem /* Match encountered, so CR+LF is good, hence return 
       rem the current buffer; the `if` query avoids to output 
       rem an empty line initially: */ 
       if defined LBUF (
        setlocal EnableDelayedExpansion 
        >> "!FILENEW!" echo(!LBUF! 
        endlocal 
       ) 
       rem // Store the current line to the buffer: 
       set "LBUF=%%L" 
      ) 
     ) 
     setlocal EnableDelayedExpansion 
    ) 
    rem // Return the remaining content of the buffer finally: 
    >> "!FILENEW!" echo(!LBUF! 
    endlocal 
) 

endlocal 
exit /B 

대문자와 소문자를 구별하지 않고로, 단순히로 /I 스위치를 추가 findstr 명령

각 (연결된) 줄의 전체 길이는 약 8190 자로 제한됩니다.

0

여러분 모두에게 감사드립니다. Dbenham, CR/LF 파일의 끝에서 좋다고 말하면 내 사양이 불완전하다고 말하는 순간 당신이 맞습니다. JREPL 링크에 감사드립니다!

Regex와 Powershell로 케이스를 해결했습니다. $ FileOut = $ fileIn -creplace '\ x0D \ x0A (?! [R] [0-9] {5} [a-z])', '. '

FileIn 읽기 옵션 : -Encoding UTF8 -Raw

+0

"감사합니다"라는 답을 게시하는 대신 가장 유용한 답을 수락하고 의견을 남기는 것을 고려해보십시오. –