Find the answer to your Linux question:
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 ...
  1. #1
    Just 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":

    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]
    So here is the script I have:

    Code:
    amixer get Master | grep % | sed 's/.*\([0-9]*\%\).*/\1/'
    But my output is a mere percent sign --
    Code:
    %
    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%"?

    Can anyone help?

  2. #2
    tpl
    tpl is offline
    Linux User
    Join Date
    Jan 2007
    Location
    cleveland
    Posts
    452
    what about "grep Mono | cut f3 <filename"
    the sun is new every day (heraclitus)

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    4
    Well, using cut, I might do:

    Code:
    amixer get Master | grep % | cut -c 21-25 | sed -e 's/\[//' -e 's/\]//'
    But that's just awkward. And it still irks me that sed isn't behaving like it should.

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    try
    Code:
    amixer get Master | egrep -o "[0-9]+%"
    if you don't have egrep, use
    Code:
    amixer get Master | grep -E -o "[0-9]+%"

  5. #5
    Just Joined!
    Join Date
    Nov 2008
    Posts
    4
    That's even better than sed. Thanks.

  6. #6
    Just Joined!
    Join Date
    Apr 2009
    Posts
    33
    Quote Originally Posted by Masterofpsi View Post
    I

    Code:
    amixer get Master | grep % | sed 's/.*\([0-9]*\%\).*/\1/'
    But my output is a mere percent sign --
    Code:
    %
    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%"?
    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 '['

  7. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    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:
    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
    producing:
    Code:
    % ./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%
    cheers, drl
    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 )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...