XMLファイルで特定のタグ部分を見つける方法は?

XMLファイルで特定のタグ部分を見つける方法は?

私のファイルの最後の数行は次のとおりです/usr/share/glib-2.0/schemas/org.gnome.Vino.gschema.xml

<schemalist>
  <schema>
   <!-- some other tags -->

    <key name='notify-on-connect' type='b'>
      <summary>Notify on connect</summary>
      <description>
        If true, show a notification when a user connects to the system.
      </description>
      <default>true</default>
    </key>

    <key name='enabled' type='b'>
      <summary>Enable remote access to the desktop</summary>
      <description>
      If true, allows remote access to the desktop via the RFB
      protocol. Users on remote machines may then connect to the
      desktop using a VNC viewer.
      </description>
      <default>false</default>
    </key>
  </schema>
</schemalist>

この段落が必要な場合grep

<key name='enabled' type='b'>
  <summary>Enable remote access to the desktop</summary>
  <description>
  If true, allows remote access to the desktop via the RFB
  protocol. Users on remote machines may then connect to the
  desktop using a VNC viewer.
  </description>
  <default>false</default>
</key>

grepこれを達成するには、このコマンドをどのように使用する必要がありますか?

ベストアンサー1

提供した例は有効なXMLファイルなので、xqXMLパーサーツールを使用してファイルを処理します。yqインストールパッケージ

xq -x --xml-root key '
    .schemalist.schema.key[] | select(."@name" == "enabled")
' infile.xml

「key」タグの「@name」属性が「enabled」と同じ場合、「key」タグが選択されます。

からxq -h

--xml-出力、-x
jq JSON出力を再びXMLにトランスコードしてエクスポートします。
--xml-rootXML_ROOT
XMLに再トランスコードするときは、出力をこの名前の要素にラップします。

おすすめ記事