Link to home
Start Free TrialLog in
Avatar of antonioking
antoniokingFlag for United Kingdom of Great Britain and Northern Ireland

asked on

return whole line that contains string

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!
Avatar of Bill Prew
Bill Prew

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
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
Avatar of antonioking

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, works spot on!
Great, glad that helped, thanks for the feedback.