Find a matching line and extract till end of

Dell / Optiplex gx270...
May 6, 2009 at 09:55:46
Specs: Microsoft Windows XP Professional, 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.


See More: Find a matching line and extract till end of

Report •


#1
May 6, 2009 at 10:28:00
@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


Report •

#2
May 6, 2009 at 12:00:15
Everything is fine , except that its not writting into the new file.

Report •

#3
May 6, 2009 at 12:29:51
It did for me.


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

M2


Report •

Related Solutions

#4
May 6, 2009 at 12:41:53
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.


Report •

#5
May 6, 2009 at 13:27:23
Jus found the problem , if !N! gtr !start! this condition is never statisfied... start is always 4 and N is always 0

Report •

#6
May 6, 2009 at 13:27:40
Is that your complete bat?


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

M2


Report •

#7
May 6, 2009 at 13:28:10
N is not gettin incremented by 1 , can u please advice.

Report •

#8
May 6, 2009 at 13:30:43
Yes thats my complete bat

Report •

#9
May 6, 2009 at 14:40:48
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


Report •

#10
May 6, 2009 at 17:37:11
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



Report •

#11
May 6, 2009 at 23:20:20
[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


Report •

#12
May 7, 2009 at 10:40:42
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 :(


Report •

#13
May 7, 2009 at 11:09:17
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 ?


Report •

#14
May 7, 2009 at 11:27:46
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 )



Report •

#15
May 7, 2009 at 13:19:42
@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


Report •


Ask Question