Extract part of a text file using batch dos

2020-02-15 01:42发布

问题:

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

Is that possible using a dos batch file ? many thanks.

回答1:

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


回答2:

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


回答3:

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