0

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?

3 Answers3

2

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
)
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Your solution execute `more` command 1000 times, and each time open the output file and seeks for EOF to append the next line. This is not efficient... – Aacini Nov 13 '12 at 16:57
1

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

A Windows user...

Community
  • 1
  • 1
Vajk Hermecz
  • 5,413
  • 2
  • 34
  • 25
  • If Microsoft made a utility for this, it would spew messages like `Found 6000 in foo.txt. Copying Line 6000. Copying line 6001. ... Copying line 6999. 1000 line(s) copied.` These would be mingled with the output, and require an option to disable them. – Kaz Oct 16 '13 at 18:56
1

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
Aacini
  • 65,180
  • 12
  • 72
  • 108