Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-01-2021, 03:55 PM   #1
Registered: Aug 2003
Posts: 409

Rep: Reputation: 31
how to apply grep using a variable?

i'm trying to use sox to probe some mp3/wav sound files, looking for output something like this:

Peak  Level (dB)       -3.80
RMS   Level (dB)      -19.74
Noise Floor (dB)      -75.00
Length (seconds)      15.256
(the output is a little different in my example above as i also pipe through sed to make the text more understandable. the mp3/wav extension is always the last thing in a line. )

this line works for mp3 files:
grep -E 'Pk lev dB|RMS lev dB|RMS Tr dB|Length s|^-|mp3$|^$'
since i'm working with both mp3 as well as wav files, though, i want to change the "mp3" call in grep to be a variable so that i can assign it as either "mp3" or "wav":
if blah blah blah
i guess i need to change the grep call
to something like
i haven't gotten anything to work. i've tried about a million combinations and no luck yet.

can someone show me what to change?


Last edited by babag; 11-01-2021 at 04:00 PM.
Old 11-01-2021, 04:17 PM   #2
Senior Member
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,224
Blog Entries: 1

Rep: Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585Reputation: 1585
Something like this:
grep -E 'Pk lev dB|RMS lev dB|RMS Tr dB|Length s|^-|'"$filetype"'$|^$'
Old 11-01-2021, 05:03 PM   #3
Senior Member
Registered: Mar 2020
Posts: 3,037

Rep: Reputation: Disabled
I still think awk as shown in your other thread is easier to read and understand. And eliminates the need to grep for mp3.

Last edited by shruggy; 11-01-2021 at 05:21 PM.
Old 11-01-2021, 05:08 PM   #4
Registered: Aug 2003
Posts: 409

Original Poster
Rep: Reputation: 31
yeah! thanks! don't think i'd ever have figured that out on my own.

thanks again,


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] assign full name to variable then grep a file for the variable socalheel Programming 3 09-16-2013 12:04 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
[SOLVED] Apply GREP to replace invalid UTF-8 characters in files SparceMatrix Programming 19 11-03-2011 07:45 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 01:08 AM
R question- how to use apply on a two variable t-test SerfurJ Programming 0 12-30-2003 03:34 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:48 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration