findstr batch

Microsoft Windows xp professional w/serv...
August 12, 2010 at 05:47:08
Specs: Windows XP
I am trying to use findstr to search through a text file. Once I find the string I am looking for I would like to be able to capture the next line in the document.

Example of text file data.txt:
HKEY_LOCAL_MACHINE\Software\Microsoft\SystemDrive%\Documents and Settings\wortley_td
0105000A6838A2FEB628283FBEA8D0100
Flags REG_DWORD 0x0

I want to capture the line after
"Settings\wortley_td"
"0105000A6838A2FEB628283FBEA8D0100"
and send it to a variable or text document.


See More: findstr batch

Report •

#1
August 12, 2010 at 10:40:59
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "this" ^< myfile') do (
set N=%%a
)
for /f "skip=%N% tokens=* delims= " %%a in (myfile) do (
set var=%%a
goto :done
)
:done
echo.!var!


=====================================
Helping others achieve escape felicity

M2


Report •

#2
August 12, 2010 at 12:51:44
What would you have to do to make it get the line before the string instead of after? (I'm just trying to understand how this works)

Report •

#3
August 12, 2010 at 22:37:23
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=1 delims=[]" %%a in ('find /n "this" ^< myfile') do (
set N=%%a
)

set /a N-=2

for /f "skip=%N% tokens=* delims= " %%a in (myfile) do (
set var=%%a
goto :done
)
:done
echo.!var!


=====================================
Helping others achieve escape felicity

M2


Report •

Related Solutions

#4
August 13, 2010 at 05:34:35
This is exactly what I needed! Thank you so much!

Report •

#5
August 30, 2010 at 03:13:25
Question to that...
what would you do if you wanted the line three down from the found string??

Report •

#6
August 30, 2010 at 14:27:35
set /a N+=2


=====================================
Helping others achieve escape felicity

M2


Report •

#7
August 30, 2010 at 17:59:28
The batch M2 provided only works for this case, but for general case, it has a flaw. It only search for last occurrence if there are more than 1 occurrence of the pattern found.


download gawk for windows then do this for getting the 2nd line after the search string found

c:\test> gawk.exe "c&&c--&&c==0{print}/search/{c=2}" myfile

and this for before the search string found

c:\test> gawk.exe "/pattern/{print p}{p=$0}" myfile

This solution does not need to go through the file more than 1 time and it will capture what you want for every line that the pattern occurs.

GNU win32 packages | Gawk


Report •

Ask Question