我想知道是否有一种简单的方法来忽略搜索结果输出让我们说像$pacmd list-modules | grep -e 'name: <module-cli-protocol-unix>' -e 'argument: '
在这种情况下,输出给出参数是否有值。
输入$pacmd list-modules
格式片段:
...
index: 5#<---not this index because the module's module-cli-protocol-unix arg is empty
name: <module-cli-protocol-unix>#<---not this name though its name is module-cli-protocol-unix but still its arg is empty
argument: <>#<---not this module's arg because it is empty
used: -1
load once: yes
properties:
module.author = ""
module.description = ""
module.version = "10.0"
index: 6
name: <module-switch-on-port-available>
argument: <>
used: -1
load once: no
properties:
index: 7
name: <module-udev-detect>
argument: <>
used: -1
load once: yes
properties:
module.author = "Lennart Poettering"
module.description = "Detect available audio hardware and load matching drivers"
module.version = "10.0"
index: 8
name: <module-bluetooth-policy>
argument: <>
used: -1
load once: yes
properties:
module.author = "Frédéric Dalleau, Pali Rohár"
module.description = "Policy module to make using bluetooth devices out-of-the-box easier"
module.version = "10.0"
index: 9 #<---this one because it is the module-cli-protocol-unix index
name: <module-cli-protocol-unix>#<---this one because its name is module-cli-protocol-unix
argument: <sink_name=module-cli_>#<---this one because module's arg is not empty
used: -1
load once: yes
properties:
module.author = ""
module.description = ""
module.version = "10.0"
index: 10
name: <module-bluez5-discover>
argument: <>
used: -1
load once: yes
properties:
module.author = "João Paulo Rechi Vita"
module.description = "Detect available BlueZ 5 Bluetooth audio devices and load BlueZ 5 Bluetooth audio drivers"
module.version = "10.0"
...
作为最终输出,最好有这样的东西:
index: N
name: <module-cli-protocol-unix>
argument: <not-empty-value>
根据确切的输入示例,排序后的输出应如下所示:
index: 9
name: <module-cli-protocol-unix>
argument: <sink_name=module-cli_>
...但是解决方案应该是动态的,以防另一个具有非空参数的模块即将被初始化...
所以我的问题是如果参数具有非空值,如何使 grep 输出名称?而且,是的,如果有一个非脚本解决方案就好了......;)。
编辑
我刚刚尝试了该解决方案,pacmd list-modules | grep -B2 'argument: <[^>]' | grep -Po 'index:.*|name: <module-cli-protocol-unix>'
但输出如下:
index: 0
index: 1
name: <module-cli-protocol-unix>
index: 25
index: 26
name: <module-cli-protocol-unix>
...这很奇怪,因为它为 2 个模块提供了 4 个索引,但每个模块必须有一个索引:S
答案1
pacmd list-modules |\
grep -B2 'argument: <[^>]' |\
grep -B1 -A1 'name: <module-cli-protocol-unix>'
- 该
-B
选项打印匹配之前的行以及匹配本身 - 该
-A
选项在匹配后打印行 - 如果空格是一个问题,可以使用 sed 删除它,例如
所以流程是:
- 列出所有模块
- 对非零参数进行过滤
- 然后过滤正确的模块名称
答案2
在您提供一些示例之前,这将不清楚,但您可以尝试:
pacmd list-modules | grep -v 'argument: <>' | grep -e 'name: <module-cli-protocol-unix>'
它首先会拒绝所有参数为空值的字符串,然后只 grep 那些具有name: <module-cli-protocol-unix>
.
答案3
如果您正在考虑argument: <>
成为一名空的争论那么这可以工作
pacmd list-modules | grep -B2 '^argument: <..*>' | grep -E '^(index|name|argument):'
样本输出:
index: 9
name: <module-cli-protocol-unix>
argument: <sink_name=module-cli_>
答案4
尝试使用以下命令并且工作正常
命令
pacmd list-modules|awk '/index: [0-9]/||/name: <.*>/||/argument: <.*?>/{print $0}'