我下面有一个字符串,即由我的其他Ansible-playbook URI模块产生的字符串,它看起来像XML,但实际上不是。它是从XML头到尾的单行字符串。
echo $log
<?xml version='1.0' encoding='UTF-8'?>\n<response>\n <data>\n <configElement>\n <elementType>access-control</elementType>\n <attribute>\n <name>realm-id</name>\n <value>Core</value>\n </attribute>\n <attribute>\n <name>realm-id</name>\n <value>REC12-CL1</value>\n </attribute>\n <attribute>\n <name>realm-id</name>\n <value>REC12-ML1</value>\n </attribute>\n <attribute>\n <name>last-modified-date</name>\n <value>2017-11-28 16:47:31</value>\n </attribute>\n </configElement>\n </data>\n <messages/>\n <links/>\n</response>\n
我如何从上述字符串中获取<realm-id>
及其下一个值<value>
。
我已经尝试过greo -o
和某些sed
cut
方法,但是没有运气。
请帮助。
预期的输出类似于
realm-id,Core
realm-id,REC12-CL1
realm-id,REC12-ML1
您应该为此使用xpath:
xmllint --xpath '//name/text()|//value/text()' xmldatafile |
awk '(NR%2)!=0{n=$0}(NR%2)==0{v=$0;print n,v}' OFS=,
realm-id,Core
realm-id,REC12-CL1
realm-id,REC12-ML1
last-modified-date,2017-11-28 16:47:31
然后,您可以使用grep或其他方法过滤此列表