Mat Mat - 1 year ago 50
Linux Question

How to get one line from a print output in linux?

I'm trying to pull one line from a subprocess.check_output but so far I have no luck. I'm running a Python script and this is my code:

output = subprocess.check_output("sox /home/pi/OnoSW/data/opsoroassistant/rec.wav -n stat", shell=True)

and this is what I get back when I run the script:

Samples read: 80000
Length (seconds): 5.000000
Scaled by: 2147483647.0
Maximum amplitude: 0.001129
Minimum amplitude: -0.006561
Midline amplitude: -0.002716
Mean norm: 0.000291
Mean amplitude: -0.000001
RMS amplitude: 0.000477
Maximum delta: 0.002930
Minimum delta: 0.000000
Mean delta: 0.000052
RMS delta: 0.000102
Rough frequency: 272
Volume adjustment: 152.409

Now I want to get the 9th line (RMS amplitude) out of this list. I already tried something with sed but it didnt gave anything back:

output = subprocess.check_output("sox /home/pi/OnoSW/data/opsoroassistant/rec.wav -n stat 2>&1 | sed -n 's#^RMS amplitude:[^0-9]*\([0-9.]*\)$#\1#p0'",stderr= subprocess.STDOUT, shell=True)

Thank You

3kt 3kt
Answer Source

What about grep-ing the line ?

output = subprocess.check_output("sox /home/pi/OnoSW/data/opsoroassistant/rec.wav -n stat 2>&1 | grep 'RMS     amplitude:'",stderr= subprocess.STDOUT, shell=True)