user1757691
user1757691

Reputation: 37

Extract section of a text file

I am trying to write a batch file that will extract lines 6000 to 6999 in a give text file. From googling I have come accross the following code - however this is giving me a blank output file.

@echo off 
SetLocal EnableDelayedExpansion
type nul > nodedata.txt
set StartText=6000
set EndText=7000
set Flag=0
for /f "tokens=* delims=" %%a in ('type out.txt') do (
if /i "%StartText%" EQU "%%a" (set Flag=1) 
if /i "%EndText%" EQU "%%a" (set Flag=0) 
if !Flag! EQU 1 echo %%a >> nodedata1.txt
)

Any ideas as to where I am going wrong?

Upvotes: 0

Views: 1007

Answers (3)

Aacini
Aacini

Reputation: 67326

This is a Batch solution that run faster...

@echo off
SetLocal EnableDelayedExpansion
set count=0
(for /F "skip=5999 delims=" %%a in (out.txt) do (
   echo %%a
   set /A count+=1
   if !count! equ 1000 goto endLoop
   )
) > nodedata1.txt
:endLoop

Upvotes: 1

Bali C
Bali C

Reputation: 31251

Here is a quick and simple pure batch solution

for /l %%a in (6000,1,6999) do (
more C:\file.txt +%%a >>C:\extracted.txt
)

Upvotes: 2

Vajk Hermecz
Vajk Hermecz

Reputation: 5732

You should install unxutils and then see answers for this question... Windows is just not made for text processing...

A Windows user...

Upvotes: 1

Related Questions