Grab a line 4 lines below seach string

May 17, 2010 at 14:44:58
Specs: Windows XP
Hello All,

This is kind of weird and I have had a heck of a time searching the net for it because I just don't know how to word it.

I'm outputting data to a text file. Then doing a find on a particular line. But what I need is once I've located the line I need to actually grab the info 2 lines below what I've looked for.

Is there a way to do that?

So what I need is to grab a couple of lines below what I actually searched for.

I'm really sorry if this is confusing.

Thanks
Eric


See More: Grab a line 4 lines below seach string

Report •


#1
May 17, 2010 at 14:52:20
Holy cow, I'm confused after reading that. Here's is my output, with any luck the formatting won't get all torched:
==================================================
Product Name : Internet Explorer
Product ID : xx
Product Key : xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
Installation Folder :
Service Pack :
Computer Name : COIT02
Modified Time : 4/17/2009 7:14:38 PM
==================================================

==================================================
Product Name : Microsoft Office Outlook 2003
Product ID : xx
Product Key : xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
Installation Folder : C:\Program Files\Microsoft Office\OFFICE11\
Service Pack :
Computer Name : COIT02
Modified Time : 4/14/2009 4:36:21 PM
==================================================

==================================================
Product Name : Office 2000
Product ID : xx
Product Key : xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
Installation Folder : c:\Program Files\Microsoft Office\Office\
Service Pack :
Computer Name : COIT02
Modified Time : 4/14/2009 4:28:25 PM
==================================================

==================================================
Product Name : Windows XP Professional
Product ID : xx
Product Key : xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
Installation Folder : C:\WINDOWS
Service Pack : Service Pack 3
Computer Name : COIT02
Modified Time : 5/13/2010 1:03:54 PM
==================================================

I'm making a script that dumps all the product keys into a .csv file so we can get a handle on our licensing. My problem is it calls all the Keys, "Product Key", So I can't search by it. However, each group has a unique "Product Name". So if I could search for that heading and then grab the Product key below it I'd be golden.

Hope that help
Eric


Report •

#2
May 17, 2010 at 17:08:00
Lol! yeah, but wait till you see this code! talk about convoluted...
@echo off & setlocal enabledelayedexpansion
:: wasn't sure if you had multiple files, or just one, so i coded
:: as an outer for-loop, not needed in case of just one textfile.
for %%z in (test) do (
for /f "tokens=1 delims=[]" %%a in ('find /n "Product Name : Windows XP"^<%%z') do (
set xx=%%a
set /a xx+=4
call :aaa !xx! %%z
)
)
goto :eof
:aaa
for /f "skip=%1 tokens=*" %%y in (%2) do (
echo %%y
goto :eof
)

Report •

#3
May 18, 2010 at 02:42:21
@echo off > newfile & setLocal enableDELAYedeXpansion

set C=

for /f "tokens=1-3* delims= " %%a in (my.txt) do (
if "%%a %%b" equ "Product Name" set /a C+=1 && set N!C!=%%d
if "%%a %%b" equ "Product Key" set K!C!=%%d
REM echo.C is !C!
)

for /L %%i in (1 1 !C!) do (
>> newfile echo.!N%%i! !K%%i!
)


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

M2


Report •

Related Solutions

#4
May 20, 2010 at 06:56:54
Thanks for the help guys, sorry it took so long for me to get back to you all.

Try as I might, I was unable to understand the hieroglyphics above :). Unfortunately for myself, you guys are so far past me I might as well be rubbing two sticks together for fire. Anyway I ended up going back to scratch and came up with the below script. Now I'm sure it's inefficient as hell, but with what I know it's the best I could come up with, and as a bonus it does seem to work. I wasn't just blatantly disregarding your help I just am not advanced enough in batch files to understand what's going on.

I was wondering if any of you could point out problems you see with it and how it could be done better (as I'm sure you all can).

Again thanks for the help, this is by far the best place I've found to get this sort of information.

Eric

Here's the code:

@echo off
:: Cleaning up before we get started
del /q /f "%temp%\produkey-dump.txt"
del /q /f "%temp%\linenumbers.txt"
del /q /f "%temp%\prodname.txt"
del /q /f "%temp%\productkey.txt"

:: DUMPING OUTPUT OF PRODUKEY.EXE (BY NIRSOFT) TO A TEXT FILE
"ProduKey.exe" /stext "%temp%\produkey-dump.txt"

:: ADDING NUMBERS TO EACH LINE
find /i /n " " "%temp%\produkey-dump.txt" > "%temp%\linenumbers.txt"

:: FINDING THE WINDOWS XP LINE NUMBER
findstr /i /c:"Windows XP" "%temp%\linenumbers.txt" > "%temp%\prodname.txt"

:: SETTING A VAR TO BE THE LINE SINGLED OUT ABOVE
set /p linefind=<"%temp%\prodname.txt"

:: SETTING THE VAR TO BE JUST THE NUMBER OF THE LINE SINGLED OUT ABOVE
set linefind=%linefind:~1,2%

:: ADDING +2 TO THE VAR SINGLED OUT ABOVE TO GET THE LINE OF THE PRODUCT KEY
:: AND ADDING BRACKETS AROUND THE SUM SO IT'S MORE UNIQUE.
set /a productkey=%linefind%+2
set productkey=[%productkey%]

:: FINDING THE PRODUCT KEY LINE AND SENDING IT TO A TEXT FILE
findstr /i /c:"%productkey%" "%temp%\linenumbers.txt" > "%temp%\productkey.txt"

:: SETTING VAR TO BE PRODUCT KEY LINE SINGLED OUT ABOVE
set /p productkey=<"%temp%\productkey.txt"

:: REMOVING UNWANTED TEXT FROM THE VAR
set xpproductkey=%productkey:~24,50%

echo %computername% %xpproductkey% >> \\server\share\coa.csv


Report •

#5
May 20, 2010 at 08:07:10
At this point I'm beginning to wonder it it wouldn't just be easier to grab the relevant information from the registry and just do the math in a batch file in order to obtain the Product keys.

Report •


Ask Question