Solved

batch script to scan a txt file and then pipe the following line out to variable

Posted on 2009-04-10
3
435 Views
Last Modified: 2012-05-06
I need a batch script that can scan the contents of a text file for a specific word and then output the text in the following line.

e.g text file contains:
rsdhkjd
dsfhkjhdsf
hjdkhkjdsf
this is useful:
       i need this line only

i need to script to scan through the text file and output the line after the 'this is useful' line

ive tried to do this with for /f but cannot make it read the next line
0
Comment
Question by:paul_at_work
3 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 24114821
Here's one possible solution:
@echo off

setlocal

set InFile=test.txt

set Search=This is useful:
 

set /a LineNumber = 0

for /f "tokens=1 delims=[]" %%a in ('type "%InFile%" ^| find /i /n "%Search%"') do set LineNumber=%%a

if %LineNumber%==0 (echo Search string not found!&goto :eof)

set /a LineNumber+=1

for /f "tokens=1* delims=[]" %%a in ('type "%InFile%" ^| find /v /n ""') do if "%%a"=="%LineNumber%" set Line=%%b

echo %Line%

Open in new window

0
 
LVL 2

Expert Comment

by:brislane
ID: 24114833
find "text you want" file.txt > output.txt
0
 

Author Closing Comment

by:paul_at_work
ID: 31568863
Thank you so much, perfect.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

We have adopted the strategy to use Computers in Student Labs as the bulletin boards. The same target can be achieved by using a Login Notice feature in Group policy but it’s not as attractive as graphical wallpapers with message which grabs the att…
It is only natural that we all want our PCs to be in good working order, improved system performance, so that is exactly how programs are advertised to entice. They say things like:            •      PC crashes? Get registry cleaner to repair it!    …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now