提取标签值的 Shell 脚本

提取标签值的 Shell 脚本

假设我有一个如下所述的 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

您能帮我提供用于执行此活动的 unixcommand/shell 脚本吗?

提前致谢!!!

答案1

Python3.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


xmlstarlet+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

使用xmlstarlet循环遍历每个serviceInstance节点:

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

这会匹配serviceInstance节点,并且对于每个这样的节点,它提取name其祖父母节点的属性、machine节点的值和status节点的值。这些输出之间有逗号 ( -o ,) ,末尾有换行符 ( -nl)。

您还可以从xq(来自https://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

相关内容