如何使用 Bash 剪切包含开头和结尾的部分?

发布于 2024-12-13 02:35:03 字数 2067 浏览 0 评论 0原文

当我做 pactl list 时,我得到了很多信息。根据这些信息,我试图只从 Sink #0 开始直到该部分结束。

1)信息

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)我正在尝试,例如:

#!/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)它输出非常奇怪

grep: dB: No such file or directory

我如何使用我的 BASH 脚本获取该部分,是否有任何其他最佳方法来进行此类过滤?

跟进:所以我也试图保持简单。例如:

pactl list | grep Volume | head -n1 | cut -d' ' -f2- | tr -d ' '
|________|   |________|    |______|   |_____________|  |_________|
  |            |                |              |           |
  command     target get    show 1 row      cut empty      Dont know..
  to list 

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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫﹏色ふ单纯 2024-12-20 02:35:03

您可以使用 sed 编辑器的多个功能来实现您的目标。

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

-n 表示“不执行打印每一行输出的标准选项

/^Sink/,/^$/ 是一个范围正则表达式,表示找到一个以 Sink 开头的行,然后继续查看行,直到找到空行 (/^$/)

p 表示打印您匹配的内容。

如果空行上有空格或制表符,请使用 " ...,/^$[${spaceChar}${tabChar}]*\$/​​p"。请注意从单引号到双引号的更改,这将允许变量 ${spaceChar} 和 ${ tabChar} 要扩展为它们的实际值,您可能需要在使用它们之前先定义 spaceChar 和 tabChar,比如 spaceChar=" " 。在这里给你看tabChar,但并非所有 sed 都支持 \t 版本,您可以选择按 Tab 键或使用 \t。 虽然使用

bash 可能可以实现您的目标,但 sed 是为此类问题而设计的。

我希望这有帮助。

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.

请远离我 2024-12-20 02:35:03

尝试:

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

代替

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

Try:

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

instead of

Part1=$(grep "Sink #0" $command);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文