1

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?

2

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hello, many thanks for your answer. Unfortunately I missed to specify that WordA and WordB will not be on the same line.
1

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

Comments

1

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

1 Comment

@btidba See this answer to your question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.