タグ値を抽出するシェルスクリプト

タグ値を抽出するシェルスクリプト

以下のように 1 つの XML ファイルがあり、UNIX コマンドを使用してアプリケーション名、マシン、ステータス タグの値を抽出し、コンマ区切り形式で表示したいとします。

XML ファイル:-

 <?xml version="1.0" encoding="UTF-8"?>
<applications>
<application name="Adapter/Code1">
<service name="Code1.par">
<deploymentStatus>Success</deploymentStatus>
<serviceInstance name="Code1-One">
    <machine>123</machine>
    <status>Running</status>
</serviceInstance>
<serviceInstance name="Code1-Two">
    <machine>456</machine>
    <status>Running</status>
</serviceInstance>
</service>
</application>
<application name="Adapter/Code2">
<service name="Code2.par">
<deploymentStatus>Success</deploymentStatus>
<serviceInstance name="Code2-One">
    <machine>123</machine>
    <status>Running</status>
</serviceInstance>
<serviceInstance name="Code2-Two">
    <machine>456</machine>
    <status>Running</status>
</serviceInstance>
</service>
</application>
</applications>

出力:-

Adapter/Code1,123,Running

Adapter/Code1,456,Running

Adapter/Code2,123,Running

Adapter/Code2,456,Running

このアクティビティを実行するための unix コマンド/シェル スクリプトを提供していただけますか?

前もって感謝します!!!

答え1

パイソン3.xソリューション(xml.etree.ElementTreeモジュール):

import xml.etree.ElementTree as ET

tree = ET.parse("test.xml")
root = tree.getroot()
for app in root.findall('application'):
    for m,s in zip(app.iter('machine'), app.iter('status')):
        print("%s,%s,%s" % (app.get('name'), m.text, s.text))

出力:

Adapter/Code1,123,Running
Adapter/Code1,456,Running
Adapter/Code2,123,Running
Adapter/Code2,456,Running

https://docs.python.org/3.6/library/xml.etree.elementtree.html?highlight=etree#module-xml.etree.ElementTree


xmlスターレット+awk(各要素の子ノードをグループ化するために使用されますapplication) 解決策:

xmlstarlet sel -t -v "//application/@name| .//machine/text()| .//status/text()" -n input.xml 
 | awk '/Adapter/{app=$0; r=app; c=0; next}
   { if(++c==2){ c=0; print r","$0; r=app } else { r=r","$0 }}'

出力:

Adapter/Code1,123,Running
Adapter/Code1,456,Running
Adapter/Code2,123,Running
Adapter/Code2,456,Running

  • "//application/@name| .//machine/text()| .//status/text()"- 必要なノードを取得するためのXPath式

  • /Adapter/{app=$0; r=app; c=0; next}- それぞれのapplication名前をキャプチャしてさらに連結する

http://xmlstar.sourceforge.net/doc/UG/xmlstarlet-ug.html

答え2

インストールキシデルxpath を使用します。

私の意見では、最良の視点は次の通りですserviceInstance

xidel f.xml -e '//serviceInstance/string-join((../../@name, machine, status),",")'
Adapter/Code1,123,Running
Adapter/Code1,456,Running
Adapter/Code2,123,Running
Adapter/Code2,456,Running

答え3

xmlstarletserviceInstanceノードをループするには、次のようにします。

xmlstarlet sel -t \
    -m '//application/service/serviceInstance' \
    -v '../../@name' -o , \
    -v 'machine' -o , \
    -v 'status' -nl \
    file.xml

これはserviceInstanceノードを照合し、各ノードについて、nameその親ノードの属性、machineノードの値、およびstatusノー​​ドの値を抽出します。これらは、間にカンマ ( -o ,) が挿入され、最後に改行 ( -nl) が付いて出力されます。

引用符付きのCSV出力も取得できますxqhttps://kislyuk.github.io/yq/):

xq -r '
    .applications.application[] | ."@name" as $name |
    .service.serviceInstance[]  | [ $name, .machine, .status ] | @csv' file.xml

答え4

XML ツールを使用しない正当な理由がある場合は、アプリケーションが例のように単純なものである限り、低レベルの解析を使用できます。

sed 's/<application name="\([^"]*\)">/\1/
Ta
h
d
:a
/<machine>/!d
G
N
s_.*<machine>\(.*\)</machine>\n\(.*\)\n.*<status>\(.*\)</status>.*_\2,\1,\3_' yourfile.xml

関連情報