0

I'm actually using using a command line tool called sox to retrieve information about an audio file. This returns the following output:

Samples read:            449718
Length (seconds):     28.107375
Scaled by:         2147483647.0
Maximum amplitude:     0.999969
Minimum amplitude:    -0.999969
Midline amplitude:     0.000000
Mean    norm:          0.145530
Mean    amplitude:     0.000291
RMS     amplitude:     0.249847
Maximum delta:         1.316925
Minimum delta:         0.000000
Mean    delta:         0.033336
RMS     delta:         0.064767
Rough   frequency:          660
Volume adjustment:        1.000

I'd like to extract the values out of this using a regular expression. So far I have /^Length \(seconds\):\s*[0-9.]*/m which matches Length (seconds): 28.107375 but I just want the value.

What do I need to do?

Sam Leicester
  • 109
  • 10

3 Answers3

1

Two options (I'm not familiar with sox, so I'm not sure how exactly this would work in sox):

  1. You can use lookbehind to match the first part. This works in case the regex engine allows variable length lookbehind: /(?<=^Length \(seconds\)\:\s*)[\d.]*/
  2. You can capture the value in a group to reference it later: /^Length \(seconds\):\s*([\d.]*)/. this will work if sox has a capture group functionality. In this case, the value will be held in the first capture group ($1 in ruby)
davidrac
  • 10,723
  • 3
  • 39
  • 71
  • Thanks for your response @davidrac, some good terminology to look into here, namely: variable length lookbehind and capture groups. The mentioning of `sox` was just for background info. I'll be executing `sox` through ruby anyway so I'm thinking ruby would be best for the regex. – Sam Leicester Aug 02 '12 at 13:43
  • This show the matched groups that you get back, very useful: [Rubular matched group for length value](http://rubular.com/r/sJ3TQHOL44) – Sam Leicester Aug 02 '12 at 14:06
1

"soxi -D filename" returns the length of filename in seconds. Just the number, nothing else. Information about SoXI can be found here

Johnzo
  • 112
  • 10
0
awk '/^Length \(seconds\):/ { print $NF }'
tripleee
  • 175,061
  • 34
  • 275
  • 318