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

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 end of

Reply to Message Icon

Name: Gods
Date: May 6, 2009 at 09:55:46 Pacific
OS: Microsoft Windows XP Professional
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.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: May 6, 2009 at 10:28:00 Pacific
Reply:

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


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 2
Name: Gods
Date: May 6, 2009 at 12:00:15 Pacific
Reply:

Everything is fine , except that its not writting into the new file.


0

Response Number 3
Name: Mechanix2Go
Date: May 6, 2009 at 12:29:51 Pacific
Reply:

It did for me.


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 4
Name: Gods
Date: May 6, 2009 at 12:41:53 Pacific
Reply:

set /a N=0
set start=

for /f "tokens=1 delims=[]" %%a in ('find /n "5/6/2009" ^< WSOData-UnattendedMode.log') do (
if not defined start set start=%%a
)

for /f "tokens=* delims=" %%a in (WSOData-UnattendedMode.log) do (
set /a N+=1
set oops=%%a
if !N! gtr !start! echo %%a>newfile.txt
)


I havent changed anythin else , except that i changed the matching string and the file name... but it doesnt seem to write or even create the new file.... even if the if condition fails we should have a file created atleast rite ?? I did a echo above the if statement adn it worked.


0

Response Number 5
Name: Gods
Date: May 6, 2009 at 13:27:23 Pacific
Reply:

Jus found the problem , if !N! gtr !start! this condition is never statisfied... start is always 4 and N is always 0


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: May 6, 2009 at 13:27:40 Pacific
Reply:

Is that your complete bat?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 7
Name: Gods
Date: May 6, 2009 at 13:28:10 Pacific
Reply:

N is not gettin incremented by 1 , can u please advice.


0

Response Number 8
Name: Gods
Date: May 6, 2009 at 13:30:43 Pacific
Reply:

Yes thats my complete bat


0

Response Number 9
Name: Gods
Date: May 6, 2009 at 14:40:48 Pacific
Reply:

Mechanix2Go ,

That loop is not executing i guess , the loop that increments 'N' , i also tried to use the example i had given and the orginal program but no luck


0

Response Number 10
Name: ghostdog
Date: May 6, 2009 at 17:37:11 Pacific
Reply:

use a good tool for text processing. eg if you have Perl on windows

c:\test> perl -ne 'print if /2004/..eof' file
2004 acsdcvxc
2005 adsdfsc
2006 adwsadfsdf



0

Response Number 11
Name: Mechanix2Go
Date: May 6, 2009 at 23:20:20 Pacific
Reply:

[1] You need your 'setup' line:

@echo off & setLocal EnableDelayedExpansion

[2] What are you trying to do with:

set oops=%%a

?


=====================================
If at first you don't succeed, you're about average.

M2


0

Response Number 12
Name: Gods
Date: May 7, 2009 at 10:40:42 Pacific
Reply:

i have my set up line and oops was jus to test. The code u gave wrks fine for that example i gave but when i try to use a file of this format it doesnt match or write to a file.


5/7/2009 12:00:19 AM: Communications Logging: False
5/7/2009 12:00:19 AM: Communications Logging Path: \
5/7/2009 12:00:36 AM: Starting subscription retrieval
5/7/2009 12:00:38 AM: Subscriptions retrieved
5/7/2009 12:00:38 AM: Starting transaction download
5/7/2009 11:00:38 AM: Transaction download complete
5/7/2009 11:00:38 AM: Import completed starting sending


and i tried to match the string 5/7/2009 this doesnt wrk :(


0

Response Number 13
Name: Gods
Date: May 7, 2009 at 11:09:17 Pacific
Reply:

I think i found the problem , since we try to get the line number of the matchin string and set it to start , the last line since it matched is set and hence nothin is printed.

As soon as i hit a matchin String i should exit out of the loop and proceed wit my second loop... hw can i do that ?


0

Response Number 14
Name: Gods
Date: May 7, 2009 at 11:27:46 Pacific
Reply:

Jus see the pattern in which start is assigned , the if loop not defined isnt wrking... its assigning start variable with a value always.

(if not defined start set start=1062 )

(if not defined start set start=1063 )

(if not defined start set start=1064 )

(if not defined start set start=1065 )

(if not defined start set start=1066 )

(if not defined start set start=1067 )

(if not defined start set start=1068 )

(if not defined start set start=1069 )

(if not defined start set start=1070 )

(if not defined start set start=1071 )

(if not defined start set start=1072 )

(if not defined start set start=1080 )



0

Response Number 15
Name: Gods
Date: May 7, 2009 at 13:19:42 Pacific
Reply:

@echo off > newfile.txt & setLocal EnableDelayedExpansion

set /a N=0
set start=

for /f "tokens=1 delims=[]" %%a in ('Find /N "5/7/2009" ^< UnattendedMode.log') do (
set start=%%a
goto aims
)


:aims

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

I tried this but N doesnt get incremented , can you please tel;l me if i have done any mistake


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Find a matching line and extract till end of

Find a matching line and extract till eof www.computing.net/answers/programming/find-a-matching-line-and-extract-till-eof-/20158.html

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

Sed:Add text to end of line in file www.computing.net/answers/programming/sedadd-text-to-end-of-line-in-file/15109.html