How do i cut section with start and end using Bash?

1.5k views Asked by At

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 
2

There are 2 answers

4
shellter On BEST ANSWER

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.

1
Jared On

Try:

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

instead of

Part1=$(grep "Sink #0" $command);