Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

return whole line that contains string

Posted on 2013-11-18
6
359 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 53

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 53

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 53

Expert Comment

by:Bill Prew
ID: 39659676
Great, glad that helped, thanks for the feedback.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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