LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 10-24-2021, 08:02 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 409

Rep: Reputation: 31
escape character in sed?


i'm using sed with sox to parse and display info about mp3 files. i've successfully been able to print a txt file using this:
Code:
for file in *.mp3

    do echo '\n--------------------\n'$file'\n'                         # Prints a line, the filename underneath, an empty line under that.
    sox $file -n stats                                                  # Prints the 'stats' output.
    
done 2>&1 | grep -E 'Pk lev dB|RMS lev dB|RMS Tr dB|^-|mp3$|^$' | sed -e "s/Pk lev dB/Peak  Level (dB)/g; s/RMS lev dB/RMS   Level (dB) /g; s/RMS Tr dB/Noise Floor (dB)/g" > stats.txt
i wanted to include a line about the file's length but am running into problems. i think the issue may have to do with the output from sox, which has this text:
Quote:
Length s
which is followed by the file's duration in seconds.

i wanted to change that to:
Quote:
Length (seconds)
but have run into trouble. it doesn't display the change in the txt file. i'm thinking that the single 's' character is messing things up but i know very little about this. here's the line i tried that is not working:
Code:
done 2>&1 | grep -E 'Pk lev dB|RMS lev dB|RMS Tr dB|Length s|^-|mp3$|^$' | sed -e "s/Pk lev dB/Peak  Level (dB)/g; s/RMS lev dB/RMS   Level (dB) /g; s/RMS Tr dB/Noise Floor (dB)/g; s/Length s/Length (seconds)/g" > stats.txt
any help getting this substitution to work is much appreciated.

thanks,
babag
 
Old 10-24-2021, 09:35 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,059

Rep: Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671
The single s isn't going to be an issue, but what's between it and the preceding text might be. By using -E you could try it as "s/Length\s+s/Length (seconds)/". That will accommodate any white-space that happens to be there - from single or multiple spaces to a <Tab>. Note that you are very exposed to output format changes from sox by including specific spaces everywhere like that. sed can handle the text selection itself, so grep is unnecessary here, but if it works and you understand it, it's good enough.
Have you run just the grep to ensure it is selecting that record ok ?.
 
Old 10-24-2021, 10:29 PM   #3
babag
Member
 
Registered: Aug 2003
Posts: 409

Original Poster
Rep: Reputation: 31
thanks for helping! much appreciated.

tried this per your suggestion (hope i got it right):
Code:
done 2>&1 | grep -E 'Pk lev dB|RMS lev dB|RMS Tr dB|Length s|^-|mp3$|^$' | sed -e "s/Pk lev dB/Peak  Level (dB)/g; s/RMS lev dB/RMS   Level (dB) /g; s/RMS Tr dB/Noise Floor (dB)/g; s/Length\s+s/Length (seconds)/g" > stats.txt
that produced this text:
Quote:
--------------------
01_TitleHere_Ch1_R1.mp3

Peak Level (dB) -3.02
RMS Level (dB) -20.05
Noise Floor (dB) -74.95
Length s 1319.576
what i was hoping for is this:
Quote:
--------------------
01_TitleHere_Ch1_R1.mp3

Peak Level (dB) -3.02
RMS Level (dB) -20.05
Noise Floor (dB) -74.95
Length (seconds) 1319.576
thanks,
babag
 
Old 10-24-2021, 10:55 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,059

Rep: Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671Reputation: 3671
Change it to "sed -E -e ..."

Last edited by syg00; 10-24-2021 at 10:56 PM. Reason: separated options
 
Old 10-24-2021, 11:08 PM   #5
babag
Member
 
Registered: Aug 2003
Posts: 409

Original Poster
Rep: Reputation: 31
thanks again!

that's got it!

babag

Last edited by babag; 10-24-2021 at 11:13 PM.
 
Old 10-25-2021, 07:58 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,037

Rep: Reputation: Disabled
If sox always outputs the data in the same order (and why shouldn't it), then I'd do it with awk
Code:
#!/bin/sh
for file in *.mp3
do
  printf '\n------------------\n%s\n' "$file"
  sox "$file" -n stats
done 2>&1 | awk '
  BEGIN {
    _[0]="Peak  Level (dB)"
    _[1]="RMS   Level (dB)"
    _[2]="Noise Floor (dB)"
    _[3]="Length (seconds)"
  }
  /^---/ { print; getline; print }
  /^(Pk lev|RMS (lev|Tr)|Length)/ && sub(/[^0-9-]+/, _[i++]" ")
  /^$/ { print; i=0 }'

Last edited by shruggy; 11-01-2021 at 05:49 PM.
 
  


Reply


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
shell: how to escape (or not escape) $ within an echo statement? Paul_N Linux - Newbie 2 04-05-2016 02:59 AM
forward slash being treated as escape character in shell? debiant Linux - General 1 07-19-2006 05:57 PM
Escape character ? juanb Linux - Newbie 2 08-31-2004 11:03 AM
Escape character not working in shell script philipz Programming 1 04-29-2004 10:58 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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