I need to grab attributes from the XML file with pure bash script.
So I have the following XML file with a root element Group
and lots of Person
elements, every of them has id
and username
attributes. id
is unique value for each element:
<?xml version="1.0" encoding="UTF-8"?>
<Group id="D_8"
main="false">
<Person id="P_0001"
email="email0001@example.com"
username="person_0001"
password="pass_0001"
active="true"/>
<Person id="P_0002"
email="email0002@example.com"
username="person_0002"
password="pass_0002"
active="true"/>
<!-- ...and hundreds of other Person elements ... -->
</Group>
And I need to use bash script to extract the id
and username
attributes into some key-value structure:
P_0001=person_0001
P_0002=person_0002
Checked other related answers, but most of them suggest to use some XML parsers like xmllint. But unfortunately I do not have them on the target machine.
Please suggest how I can achieve this.
As long as the username
attribute does not come before id
attribute, this is a bash script to give the result:
#/usr/bin/env bash
id='\bid="([^"]+)"'
username='\busername="([^"]+)"'
while IFS= read -r line; do
[[ $line =~ $id ]] && idv="${BASH_REMATCH[1]}"
[[ $line =~ $username ]] && echo "$idv=${BASH_REMATCH[1]}"
done < data.xml
exit 0
It works even when username
attribute and id
attribute are on the same line.