bash脚本提取XML数据转换成列的格式 [英] bash script extract XML data into column format
问题描述
试图从多个字符串输出动态(数据变化)xml数据提取到一个列的格式。
约100这些XML位的回声,当我运行针对SQL数据库的查询。
<?XML版本=1.0&GT?;\r
<连接GT&;\r
< ConnectionType>腻子< / ConnectionType>\r
< CreatedBy>有人< / CreatedBy>\r
&所述; CreationDateTime> 2014-10-27T11:53:59.8993492-04:00所述; / CreationDateTime>\r
<活动与GT;\r
< OpenCommentPrompt>真< / OpenCommentPrompt>\r
< WarnIfAlreadyOpened>真< / WarnIfAlreadyOpened>\r
< /新闻与GT;\r
<组>云服务的客户交付\\威利斯\\ Linux的\\测试 - SJC< /组>\r
<&ID GT; 77e96d52-f165-482f-8389-ffb95b9d8ccd< / ID>\r
<&的KeyboardHook GT; InFullScreenMode< /&的KeyboardHook GT;\r
<元信息/>\r
<名称>主机名-H-A10D< /名称>\r
<&OpenEmbedded的GT;真< / OpenEmbedded的>\r
< PinEmbeddedMode>假LT; / PinEmbeddedMode>\r
<&腻子GT;\r
< PortFowardingArray />\r
<脚本/>\r
< SessionHost>&10.0.0.100 LT; / SessionHost>\r
<&SESSIONNAME GT;&10.0.0.100 LT; / SESSIONNAME>\r
< TelnetEncoding>&IBM437 LT; / TelnetEncoding>\r
< /腻子>\r
< ScreenColor> C24Bits< / ScreenColor>\r
< SoundHook> DoNotPlay< / SoundHook>\r
<邮票及GT; 771324d1-0c59-4f12-b81e-96edb5185ef7< / Stamp和GT;\r
< /连接>
\r
和我需要的是和列的格式。而且基本上那里的主机名相同的主机名-H-A10D,我希望能够到D末匹配和标记与开发,Q的第一列的测试,并在年底正式版为无字母。所以输出会是什么样子 - >
主机名开发-H-A10D 10.0.0.100\r
开发主机名-H-A11D 10.0.0.101\r
督促主机名-H-A12 10.0.0.201\r
测试主机名-H-A13Q 10.0.0.10
\r
我已经打得四处SED / AWK /等,并不仅仅是不能没有写出临时平面文件得到我想要的格式。我想preFER要进使用类似xmlstarlet或xmllint数组这一点。当然,更好的建议可以进行,这就是为什么我在这里:)感谢乡亲。
这将是更好地使用XML解析器。
使用 AWK
:
$ awk的-F'[<>]''BEGIN {一[D] =开发;一[Q] =测试} /名称/ {名称= $ 3; TYPE = A [SUBSTR(名称,长度(名))];如果(长度(类型)== 0)类型=正式版;} / SessionHost / {打印类型,名称,$ 3;}'s.xml
开发主机名-H-A10D 10.0.0.100
工作原理
-
BEGIN {一[D] =开发;一[Q] =测试}
此定义关联数组
A
。 -
/姓名/名称{= $ 3; TYPE = A [SUBSTR(名称,长度(名))];如果(长度(类型)== 0)型=正式版;}
在具有主机名的线,这抓住了主机名,并从它决定了主机类型。
-
/ SessionHost / {打印类型,名称,$ 3;}
在包含该主机IP线路,此打印类型,名称和IP
Trying to extract xml data from multiple string outputs dynamically (the data changes) into a column format.
About 100 of these XML bits echo out when I run a query against an SQL database.
<?xml version="1.0"?>
<Connection>
<ConnectionType>Putty</ConnectionType>
<CreatedBy>Someone</CreatedBy>
<CreationDateTime>2014-10-27T11:53:59.8993492-04:00</CreationDateTime>
<Events>
<OpenCommentPrompt>true</OpenCommentPrompt>
<WarnIfAlreadyOpened>true</WarnIfAlreadyOpened>
</Events>
<Group>Cloud Services Client Delivery\Willis\Linux\Test - SJC</Group>
<ID>77e96d52-f165-482f-8389-ffb95b9d8ccd</ID>
<KeyboardHook>InFullScreenMode</KeyboardHook>
<MetaInformation />
<Name>Hostname-H-A10D</Name>
<OpenEmbedded>true</OpenEmbedded>
<PinEmbeddedMode>False</PinEmbeddedMode>
<Putty>
<PortFowardingArray />
<Scripting />
<SessionHost>10.0.0.100</SessionHost>
<SessionName>10.0.0.100</SessionName>
<TelnetEncoding>IBM437</TelnetEncoding>
</Putty>
<ScreenColor>C24Bits</ScreenColor>
<SoundHook>DoNotPlay</SoundHook>
<Stamp>771324d1-0c59-4f12-b81e-96edb5185ef7</Stamp>
</Connection>
And what I need is the and in a column format. And essentially where the hostname equal Hostname-H-A10D, I want to be able to match the D at the end and mark the First column with Dev, Q as Test and no letter at the end as Prod. So the output would look like -->
Dev Hostname-H-A10D 10.0.0.100
Dev Hostname-H-A11D 10.0.0.101
Prod Hostname-H-A12 10.0.0.201
Test Hostname-H-A13Q 10.0.0.10
I have played around with sed/awk/etc and not just cannot get the format I want without writing out temp flat files. I would prefer to get this into an array using something like xmlstarlet or xmllint. Of course better suggestions can be made and that is why I am here :) Thanks folks.
It would be better to use an XML parser.
Using awk
:
$ awk -F'[<>]' 'BEGIN{a["D"]="Dev";a["Q"]="Test"} /Name/{name=$3; type=a[substr(name,length(name))]; if (length(type)==0) type="Prod";} /SessionHost/{print type, name, $3;}' s.xml
Dev Hostname-H-A10D 10.0.0.100
How it works
BEGIN{a["D"]="Dev";a["Q"]="Test"}
This defines associative array
a
./Name/{name=$3; type=a[substr(name,length(name))]; if (length(type)==0) type="Prod";}
On the line that has the host name, this captures the host name and, from it, determines the host type.
/SessionHost/{print type, name, $3;}
On the line that contains the host IP, this prints the type, name, and IP.
这篇关于bash脚本提取XML数据转换成列的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!