Reputation: 1500
For command,
amixer get PCM playback
I got output like this:
"Simple mixer control 'PCM',0
Capabilities: pvolume penum
Playback channels: Front Left - Front Right
Limits: Playback 0 - 255
Mono:
Front Left: Playback 255 [100%] [0.00dB]
Front Right: Playback 255 [100%] [0.00dB]"
Now I need value "100%" in a variable; so, how can I parse it?
I am getting [100
with this:
amixer get PCM playback | grep "\[" | cut -d '%' -f1 | awk '{print $5}'
Upvotes: 0
Views: 4134
Reputation: 40718
Using Gnu grep and Perl regex with positive lookahead and lookbehind, you can get the left-speaker value as
grep -Po '(?<=Left: Playback 255 \[)[^]]*(?=%\])'
Upvotes: 1
Reputation: 41456
Here is one way to do it.
Store left value in variable left
left=$(amixer get PCM playback | awk -F"[%[]" '/Left:/ {print $2}')
Store right value in variable right
right=$(amixer get PCM playback | awk -F"[%[]" '/Right:/ {print $2}')
Or store both data in in variable db
db=$(amixer get PCM playback | awk -F"[%[]" '/dB/ {print $2}')
echo "$db"
100
100
Upvotes: 2
Reputation: 753615
First off, there are two 100%
numbers in the sample output; which did you want? If they're always 100%
, then you can simply assign var=100
, so presumably they can sometimes be less than 100%
.
It looks a bit like a case for sed
, such as:
amixer get PCM playback | sed -n '/.*\[\([0-9]*\)%].*/s//\1/p'
The sed
command should give you two numbers on two lines of output, with front left value preceding front right.
You collect the output from sed
in a variable using Command Substitution:
playback=$(amixer get PCM playback | sed -n '/.*\[\([0-9]*\)%].*/s//\1/p')
This will give the two numbers in playback
. You might prefer to use an array instead:
playback=( $(amixer get PCM playback | sed -n '/.*\[\([0-9]*\)%].*/s//\1/p') )
Now you can use:
left=0
right=1
echo "Left = ${playback[$left]}"
echo "Right = ${playback[$right]}"
This assumes you are using bash
as your shell.
Upvotes: 3