Usually you should not parse Xml files using bash script. But sometimes you need to get a value from file which you know it is always there like version.

It is easier to do this if there is a unique element in whole file but if the element is a child element for a repeating element, it will be trickier to get value.

suppose that with given the xml structure below you want to extract version.


<element name="name">
<element name="version">
<element name="path">

I tried with different bash commandos. The problem was that not all of them was supported in different versions. At last I used the below script with just sed and it should be supported in older versions.

#!/usr/bin/env bash
element=$(sed -n '/version/,/element/p' ./inputfile.xml)
version=$( echo $element| sed 's/.*value>\(.*\)<\/value.*/\1/')
echo "$version"

It worked for me. Hope it works for you

Lämna ett svar

E-postadressen publiceras inte. Obligatoriska fält är märkta *