Extract part of a text file using Windows batch

前端 未结 3 930
梦毁少年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
    

提交回复
热议问题