Extract part of a text file using Windows batch

前端 未结 3 929
梦毁少年i
梦毁少年i 2020-12-22 01:59

I have a text file and I\'m interested to have only a part of it, all that is included between wordA and wordB

Is that possible using a cmd batch file?

相关标签:
3条回答
  • 2020-12-22 02:03

    Here's one way to do it. Assuming that wordA and wordB are on the same line (both must be present). The subroutine :FindString removes undesired preceding text and then replaces "wordB" with a character that we don't expect in the text that we will then use as a delimiter (` in this case). use another character if necessary.

    @ECHO OFF
    SET InFile=Test.txt
    
    FOR /F "tokens=*" %%A IN ('FINDSTR "wordA" "%InFile%" ^| FINDSTR "wordB"') DO CALL :FindString "%%A"
    pause
    GOTO :eof
    
    :FindString
    SET String=%~1
    SET String=%String:*wordA =%
    SET String=%String: wordB=`%
    FOR /F "tokens=1 delims=`" %%A IN ('ECHO.%String%') DO ECHO.%%A]
    GOTO :eof
    
    0 讨论(0)
  • 2020-12-22 02:09

    Here's a way to do it when "wordA" and "wordB" are not on the same line. It sends output to Output.txt. That will be the text between the first "wordA" and the first "wordB" in the input file (case sensitive). You didn't specify what to do if there are multple (or mismatched) sets of wordA/B.

    :RemoveWordB replaces "wordB" with a character that we don't expect in the text that we will then use as a delimiter (` in this case). use another character if necessary.

    @ECHO OFF
    SET InFile=Test.txt
    SET OutFile=Output.txt
    IF EXIST "%OutFile%" DEL "%OutFile%"
    SET TempFile=Temp.txt
    IF EXIST "%TempFile%" DEL "%TempFile%"
    
    FOR /F "tokens=*" %%A IN ('FINDSTR /N "wordA" "%InFile%"') DO (
       CALL :RemovePrecedingWordA "%%A"
       FOR /F "tokens=1 delims=:" %%B IN ('ECHO.%%A') DO (
          MORE +%%B "%InFile%"> "%TempFile%"
          FINDSTR /V "wordB" "%TempFile%">> "%OutFile%"
          FOR /F "tokens=*" %%C IN ('FINDSTR "wordB" "%InFile%"') DO (
             CALL :RemoveWordB "%%C"
             IF EXIST "%TempFile%" DEL "%TempFile%"
             GOTO :eof
             )
          )
       )
    GOTO :eof
    
    :RemovePrecedingWordA
    SET String=%~1
    SET String=%String:*wordA =%
    ECHO.%String%> "%OutFile%"
    GOTO :eof
    
    :RemoveWordB
    REM Replace "wordB" with a character that we don't expect in text that we will then use as a delimiter (` in this case)
    SET LastLine=%~1
    SET LastLine=%LastLine:wordB=`%
    FOR /F "tokens=1 delims=`" %%A IN ('ECHO.%LastLine%') DO ECHO.%%A>> "%OutFile%"
    GOTO :eof
    
    0 讨论(0)
  • 2020-12-22 02:21

    This uses a helper batch file called repl.bat from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855

    Put repl.bat in the same folder as the batch file.

    @echo off
    type infile |repl ".*wordA(.*)wordB.*" "$1" >outfile
    
    0 讨论(0)
提交回复
热议问题