简单的XML文本解析

用户名

有人可以告诉我获得上述输出的最佳方法是什么吗?我尝试使用awk和print,但不知道如何正确制作语法。

输入:

<client model="c" name="adam" desc="desc1" prot="prot1" proe="proe1" au="0" thid="id_0x7f24b00ff1f0">
<request_ccd="0100" serv="123" emt="48" emtha="48" answ="0100"></request>
<times login="2014-07-06T14:25:23+0200" uptime="18285" idle="6"></times>
<connection_ip1="1.1.1.1" port="33612">OK</connection>
</client>
<client model="c" name="john" desc="desc2" prot="prot2" proe="proe2" au="0" thid="id_0x7f24b0038950">
<request_ccd="0200" serv="234" emt="237" emtha="13" answ="0200"></request>
<times login="2014-07-06T14:41:00+0200" uptime="17348" idle="4"></times>
<connection_ip2="2.2.2.2" port="37468">OK</connection>
</client>
<client model="c" name="james" desc="desc3" prot="prot3" proe="proe3" au="0" thid="id_0x7f24b0030b10">
<request_ccd="0300" serv="345" emt="299" emtha="22" answ="0300cyfr"></request>
<times login="2014-07-06T14:45:01+0200" uptime="17107" idle="6"></times>
<connection_ip3="3.3.3.3" port="46948">OK</connection>
</client>

预期产量:

adam    prot1   1.1.1.1 0100 123
john    prot2   2.2.2.2 0200 234
james   prot3   3.3.3.3 0300 345
克里斯·西摩

设置FSRS仔细显示GNU awkoff对于这种问题的强大功能

$ awk '{print $4,$8,$32,$16,$18}' FS='"' RS='</client>' file
adam prot1 1.1.1.1 0100 123
john prot2 2.2.2.2 0200 234
james prot3 3.3.3.3 0300 345

如果您的实际XML与示例不同,那么以下脚本将有助于获取所需的字段号:

$ awk 'NR==1{for(i=1;i<=NF;i++)print i,$i}' FS='"' RS='</client>' file

这将枚举您的第一条记录中的字段,使您可以选择所需的字段。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章