I have this xml file.
<body>
<part1>
<para1>abc</para1>
<para2>def</para2>
<ver>1234</ver>
</part1>
</body>
I need to store the value given by ver i.e. 1234 in a variable.
I have this xml file.
<body>
<part1>
<para1>abc</para1>
<para2>def</para2>
<ver>1234</ver>
</part1>
</body>
I need to store the value given by ver i.e. 1234 in a variable.
Different options:
xmlstarlet:ver=$(xmlstarlet sel -t -m //ver -v . test.xml)
xmllint (see also Native shell command set to extract node value from XML:ver=$(xmllint --xpath "//ver/text()" test.xml)
gawk:ver=$(gawk -F "[><]" '/<ver>/{ print $3 }' test.xml)
awk -F'[<>]' '$2=="ver"{ print $3 }'. That would not fail if <ver> just happened to exist in some other string in the input.