Results 1 to 7 of 7
I'm trying to write a sed script that will output the volume of my Master channel using amixer. This is the output from "amixer get Master":
Code:
Simple mixer control ...
- 07-13-2009 #1Just Joined!
- Join Date
- Nov 2008
- Posts
- 4
Strange problem with sed
I'm trying to write a sed script that will output the volume of my Master channel using amixer. This is the output from "amixer get Master":
So here is the script I have:Code:Simple mixer control 'Master',0 Capabilities: pvolume pvolume-joined pswitch pswitch-joined Playback channels: Mono Limits: Playback 0 - 64 Mono: Playback 64 [100%] [0.00dB] [on]
But my output is a mere percent sign --Code:amixer get Master | grep % | sed 's/.*\([0-9]*\%\).*/\1/'
It seems to me like the \1 should be a reference to zero or more numbers followed by a percent sign. Since regexes are greedy, shouldn't that tak up all of "100%"?Code:%
Can anyone help?
- 07-14-2009 #2Linux User
- Join Date
- Jan 2007
- Location
- cleveland
- Posts
- 452
what about "grep Mono | cut f3 <filename"
the sun is new every day (heraclitus)
- 07-14-2009 #3Just Joined!
- Join Date
- Nov 2008
- Posts
- 4
Well, using cut, I might do:
But that's just awkward. And it still irks me that sed isn't behaving like it should.Code:amixer get Master | grep % | cut -c 21-25 | sed -e 's/\[//' -e 's/\]//'
- 07-14-2009 #4Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
try
if you don't have egrep, useCode:amixer get Master | egrep -o "[0-9]+%"
Code:amixer get Master | grep -E -o "[0-9]+%"
- 07-14-2009 #5Just Joined!
- Join Date
- Nov 2008
- Posts
- 4
That's even better than sed. Thanks.
- 07-15-2009 #6Just Joined!
- Join Date
- Apr 2009
- Posts
- 33
To answer your question about sed.
This:
sed 's/.*\([0-9]*\%\).*/\1/'
removes all characters before '%'. You should have had this instead:
amixer get Master | grep % | sed 's/^[^\[]*\[\([0-9]*\%\).*/\1/'
which removes everything from the beginning of the line up to '['
- 07-15-2009 #7Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
As vonbiber noted, the initial ".*" is very greedy, matching everything up to the "%", and changes to the sed command can allow the match to succeed. In this case, a very short string could be added before the number string requirement. For example, adding the (escaped) "[" to the pattern:
producing:Code:#!/usr/bin/env bash # @(#) s1 Demonstrate small change to sed command. echo set +o nounset LC_ALL=C ; LANG=C ; export LC_ALL LANG echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG" echo "(Versions displayed with local utility \"version\")" version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) sed set -o nounset echo FILE=${1-data1} echo " Data file $FILE:" cat $FILE echo echo " Results:" grep % $FILE | sed 's/.*\[\([0-9]*\%\).*/\1/' exit 0
cheers, drlCode:% ./s1 Environment: LC_ALL = C, LANG = C (Versions displayed with local utility "version") OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64 Distribution : Debian GNU/Linux 5.0 GNU bash 3.2.39 GNU sed version 4.1.5 Data file data1: Simple mixer control 'Master',0 Capabilities: pvolume pvolume-joined pswitch pswitch-joined Playback channels: Mono Limits: Playback 0 - 64 Mono: Playback 64 [100%] [0.00dB] [on] Results: 100%
Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )


Reply With Quote
