Tom Xue Tom Xue - 1 month ago 5
Bash Question

How to get the number from the script's result by shell command?

I run a command and get below result:

numid=181,iface=MIXER,name='pcm1_in Gain 0 Volume'
; type=INTEGER,access=rw---R--,values=2,min=-1440,max=360,step=0
: values=-360,-360
| dBscale-min=-144.00dB,step=0.10dB,mute=0


I need to get the value -360, and this value can vary from -1440 to 360. How to use some shell commands and regular expression to do it? Thanks!

Answer

You can pipe your command to sed.

For one line output:

yourcommand | sed -n 's/.*: values=\(-*[^,]*\),.*/\1/p'

For multiline output:

yourcommand | sed -n ':a;$!N;s/\n/ /;ta;s/.*: values=\(-*[^,]*\),.*/\1/p'

The string between : values= and , is captured and output using backreference.