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.

@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

It did for me.
=====================================
If at first you don't succeed, you're about average.M2

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.

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

Is that your complete bat?
=====================================
If at first you don't succeed, you're about average.M2

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

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

[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

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 :(

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 ?

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 )

@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
)
:aimsfor /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

![]() |
![]() |
![]() |

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