?
Solved

return whole line that contains string

Posted on 2013-11-18
6
Medium Priority
?
362 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
[X]
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
  • 3
  • 2
6 Comments
 
LVL 56

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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 56

Accepted Solution

by:
Bill Prew earned 2000 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 56

Expert Comment

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

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

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…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 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