Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 adwsadfsdfIf 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
)

@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 ) )

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 felicityM2

Hi IVO,
I knew there msut be a slicker way to do it. But didn't know how.
LOL
=====================================
Helping others achieve escape felicityM2

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |