-1

i have below string i.e., produced by my other Ansible-playbook URI module, which looks like XML but not actually. It is single line string starting from XML header to tail.

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

how can i fetch <realm-id> and its very next value <value> from above string.

i have already tried greo -o & some sed cut methods, but no luck.

please help.

expected output is something like

realm-id,Core
realm-id,REC12-CL1
realm-id,REC12-ML1
0

1 Answer 1

1

You should use xpath for this:

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

Then, you can filter this list with grep or whatever

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.