Computing.Net > Forums > Programming > Find a matching line and extract till eof

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Find a matching line and extract till eof

Reply to Message Icon

Name: Gods
Date: October 19, 2009 at 04:07:56 Pacific
OS: Windows XP
CPU/Ram: 2.992 GHz / 1021 MB
Product: Dell / Optiplex gx270
Subcategory: Batch
Comment:

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
)



Sponsored Link
Ads by Google

Response Number 1
Name: IVO
Date: October 19, 2009 at 05:40:31 Pacific
Reply:

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


0

Response Number 2
Name: Mechanix2Go
Date: October 19, 2009 at 06:01:45 Pacific
Reply:

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


0

Response Number 3
Name: Mechanix2Go
Date: October 19, 2009 at 06:06:03 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Find a matching line and extract till eof

Find a matching line and extract till end of www.computing.net/answers/programming/find-a-matching-line-and-extract-till-end-of-/18943.html

Cut few text in a matched line www.computing.net/answers/programming/cut-few-text-in-a-matched-line/18620.html

read data from a file into an array www.computing.net/answers/programming/read-data-from-a-file-into-an-array/11140.html