Solved

return whole line that contains string

Posted on 2013-11-18
6
357 Views
Last Modified: 2013-11-19
I have a text file, I'm creating a batch script that will search the text file for the word "<channels>" and return the entire line as a string.
I then want to trim <channels> and </channels> and any spaces from the string so I'm left with just the value inside the <channels> wrap.

I'd be very grateful for some help!
0
Comment
Question by:antonioking
  • 3
  • 2
6 Comments
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39658234
Here's a small BAT that should do what you described, adjust the file names at the top and give it a try.

@echo off
setlocal EnableDelayedExpansion

set Infile=in.txt
set Outfile=out.txt

for /f "tokens=*" %%A in ('find /i "<channels>" ^< "%Infile%"') do (
  set "line=%%A"
  set "line=!line:<channels>=!"
  set "line=!line:</channels>=!"
  call :Trim !line!
  echo [!Trim!]
)

:Trim
  set Trim=%*
  exit /b

Open in new window

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39658498
depending upon whether there is anything else that could be o that line, or if line could  split onto two lines I was going to use slightly simpler to use <> as delimeters?  Looks like Bill has you an answer anyway though now.

steve
0
 

Author Comment

by:antonioking
ID: 39659229
I'd like to set trim as variable I can use later in the script.
How would I do that with this script?
I've tried SET AUDIOCHANNELS = %TRIM% but it's always set to nothing.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 39659411
Try this.  Keep in mind this assumes thewre is only one of those lines in the file, otherwise it will only show the last value found.

@echo off
setlocal EnableDelayedExpansion

set Infile=in.txt
set Outfile=out.txt

for /f "tokens=*" %%A in ('find /i "<channels>" ^< "%Infile%"') do (
  set "line=%%A"
  set "line=!line:<channels>=!"
  set "line=!line:</channels>=!"
  call :Trim !line!
  set AudioChannels=!Trim!
)

echo %AudioChannels%
exit /b

:Trim
  set Trim=%*
  exit /b

Open in new window

~bp
0
 

Author Closing Comment

by:antonioking
ID: 39659525
Thanks, works spot on!
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 39659676
Great, glad that helped, thanks for the feedback.
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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

25 Experts available now in Live!

Get 1:1 Help Now