Solved

return whole line that contains string

Posted on 2013-11-18
6
355 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 51

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 51

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 51

Expert Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

12 Experts available now in Live!

Get 1:1 Help Now