Find a matching line and extract till eof

Dell / Optiplex gx270...
October 19, 2009 at 04:07:56
Specs: Windows XP, 2.992 GHz / 1021 MB
I need to match a string with a line in the file and extract all the lines after this matching line and write it to a new file.

eg :

2001 scvdsgvcxv
2002v vzvcxxzxc
2003 cxz
2004 acsdcvxc
2005 adsdfsc
2006 adwsadfsdf

If my matchin string is "2004" then i need all the lines after 2004 till end of file.I tried to use the below code but i am not successfull the N variable doesnt get incremented .

@echo off > newfile & setLocal EnableDelayedExpansion

set /a N=0
set start=

for /f "tokens=1 delims=[]" %%a in ('find /n "2004" ^< myfile') do (
if not defined start set start=%%a
)

for /f "tokens=* delims=" %%a in (myfile) do (
set /a N+=1
if !N! gtr !start! echo %%a >> newfile
)


See More: Find a matching line and extract till eof

Report •


#1
October 19, 2009 at 05:40:31
@echo off & setlocal EnableDelayedExpansion > file.new
set string=2004
set start=
for "delims=" %%j in (file.txt) do (
  if defined start (
    echo.%%j>> file.new
  ) else (
    set row=%%j
    set row=!row:%string%=!
    if not "%%j"=="!row!" set start=ON
  )
)


Report •

#2
October 19, 2009 at 06:01:45
Your code worked for me.

A couple nit-picky comments.

No need for 'if not defined' because you already cleared it.

'set /a N=0' could just as well be 'set N='


=====================================
Helping others achieve escape felicity

M2


Report •

#3
October 19, 2009 at 06:06:03
Hi IVO,

I knew there msut be a slicker way to do it. But didn't know how.

LOL


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions


Ask Question