grep - null 値を無視

grep - null 値を無視

検索結果の出力を無視する簡単な方法があるかどうか疑問に思います。$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_>

...しかし、null以外のパラメータを持つ他のモジュールが初期化されようとしている場合に備えて、ソリューションは動的でなければなりません...

だから私の質問は、引数にnull以外の値がある場合、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つのインデックスを提供するので、奇妙ですが、モジュールごとに1つのインデックスが必要です。 S

ベストアンサー1

pacmd list-modules |\
grep -B2 'argument: <[^>]' |\
grep -B1 -A1 'name: <module-cli-protocol-unix>'
  • この-Bオプションは、一致項目自体だけでなく、一致項目の前の行も印刷します。
  • この-Aオプションは、一致後に行を印刷します。
  • スペースが問題の場合は、sedを使用してスペースを削除できます。

したがって、プロセスは次のようになります。

  • すべてのモジュールを一覧表示
  • 0以外のパラメータでフィルタリング
  • 次に、正しいモジュール名をフィルタリングします。

おすすめ記事