Batch file to extract block of text in a .txt

October 18, 2011 at 13:20:23
Specs: Windows 7/Vista
Ive been searching for hours across the web to my question. I have basic knowledge in batch scripting but cannot put together anything this advanced so I desperatly need help.

I have a file, a report, that contains numerous sections in it. Each section is indicated by set of words. The set of words I am looking for in this large text file is "Replication show all". I want to be able to find that text and have it copy all the information from that line down 10 lines. This text is not in the same spot every report (i.e. line 50 in every report) so would have to search for the "Replication show all" text in order to start the read. After this 10 lines of text has been copied id like it to be written to another blank text file.

I have read that in order to do this we would have to enumerate each line so each line has an identifier to use to calculate X lines down? Not sure how it would work or even remember the command.

Im at a roadblock so im reaching out to a knowledgeable community and you all seemed the best for what I need. Id appreciate any and all comments here. Thank you.

See More: Batch file to extract block of text in a .txt

Report •

October 18, 2011 at 17:36:44
Please test this. The script is written in XP and untested in Win 7 or Vista. From the information posted I have assumed that the given string is the total content of a line.

@echo off>output.txt
setlocal enabledelayedexpansion

set string=Replication show all

for /f "tokens=*" %%1 in (input.txt) do (
    if !flag! equ 1 (
         echo %%1>>output.txt
         set /a count+=1
         if !count! gtr 9 goto endit

    if /i "%%1" equ "!string!" (set flag=1)
echo "%string%" not found check spellings and input file.
exit /b

type output.txt

Please come back & tell us if your problem is resolved.

Report •
Related Solutions

Ask Question