2

When i am doing pactl list i get lot of information. Out of those information, i am trying to only get the part start with Sink #0 till end of that section.

1) Information's

Sink #0
    State: SUSPENDED
    Name: auto_null
    Description: Dummy Output
    Driver: module-null-sink.c
    Sample Specification: s16le 2ch 44100Hz
    Channel Map: front-left,front-right
    Owner Module: 14
    Mute: no
    Volume: 0:   0% 1:   0%
            0: -inf dB 1: -inf dB
            balance 0.00
    Base Volume: 100%
                 0.00 dB
    Monitor Source: auto_null.monitor
    Latency: 0 usec, configured 0 usec
    Flags: DECIBEL_VOLUME LATENCY 
    Properties:
        device.description = "Dummy Output"
        device.class = "abstract"
        device.icon_name = "audio-card"

Source #0
    State: SUSPENDED
    Name: auto_null.monitor
    Description: Monitor of Dummy Output
    Driver: module-null-sink.c
    Sample Specification: s16le 2ch 44100Hz
    Channel Map: front-left,front-right
    Owner Module: 14
    Mute: no
    Volume: 0:  80% 1:  80%
            0: -5.81 dB 1: -5.81 dB
            balance 0.00
    Base Volume: 100%
                 0.00 dB
    Monitor of Sink: auto_null
    Latency: 0 usec, configured 0 usec
    Flags: DECIBEL_VOLUME LATENCY 
    Properties:
        device.description = "Monitor of Dummy Output"
        device.class = "monitor"
        device.icon_name = "audio-input-microphone"

2) I am trying, such as:

#!/bin/bash
command=$(pactl list);
# just get Sink #0 section not one line 
Part1=$(grep "Sink #0" $command);
for i in $Part1
do
  # show only Sink #0 lines 
  echo $i;
done

3) It output very strange

grep: dB: No such file or directory

How can i get that section using my BASH script, is there any other best way to work on such filtering?

Follow up: So i was also trying to keep it simple. such as:

pactl list | grep Volume | head -n1 | cut -d' ' -f2- | tr -d ' '
|________|   |________|    |______|   |_____________|  |_________|
  |            |                |              |           |
  command     target get    show 1 row      cut empty      Dont know..
  to list 
  • turn on shell debugging with `set -vx` on a new 2nd line of your script, then you can see what value is in $command and you'll now if things are working the way you thing they are/should be. Good luck. – shellter Oct 31 '11 at 16:39
  • excellent post and follow-up! (and thanks for accept vote!) Good luck. – shellter Nov 01 '11 at 13:34

2 Answers2

2

You can use several features of the sed editor to achieve your goal.

 sed -n '/^Sink/,/^$/p'  pactl_Output.txt

-n says "don't perform the standard option of printing each line of output

/^Sink/,/^$/ is a range regular expr, that says find a line that begins with Sink, then keep looking at lines until you find an empty line (/^$/).

the final char, p says Print what you have matched.

If there are spaces or tabs on the empty line, use " ...,/^$[${spaceChar}${tabChar}]*\$/p". Note the change from single quoting to dbl-quoting which will allow the variables ${spaceChar} and ${tabChar} to be expanded to their real values. You may need to escape the closing '$'. YOu'll need to define spaceChar and tabChar before you use them, like spaceChar=" " . No way here on S.O. for you to see the tabChar, but not all sed's support the \t version. It's your choice to go with pressing tab key or use \t. I would go with tab key as it is more portable.

While it is probably possible to accomplish your goal with bash, sed was designed for this sort of problem.

I hope this helps.

shellter
  • 36,525
  • 7
  • 83
  • 90
  • If the shell is bash you can use `$'\t'` for a tab character. Thus `sed -e 's/'$'\t''//g;'` would remove all tabs in a file. – sorpigal Oct 31 '11 at 17:10
  • Posible to make easy with pipes? e.g: $ pactl list | grep Volume | head -n1 | cut -d' ' -f2- | tr -d ' ' –  Nov 01 '11 at 08:13
  • 2
    Yes, `pactl list | sed -n '/^Sink/,/^$/p'` – tripleee Nov 01 '11 at 08:26
  • Thanks, exactly how i wanted its much safer and better. e.g: `pactl list | sed -n '/^Sink/,/^$/p' | grep Volume | head -n1 | cut -d' ' -f2- | tr -d ' '` –  Nov 01 '11 at 10:00
0

Try:

Part1=`echo $command | grep "Sink #0"`

instead of

Part1=$(grep "Sink #0" $command);
Jared
  • 1,887
  • 3
  • 20
  • 45
  • Unnecessary use of echo: `grep "Sink #0" <<<"$command"` - although even so it doesn't really solve the problem. – sorpigal Oct 31 '11 at 17:12