Reputation: 3
I need help for some command for handle the following situation. I search on net for help but i can't succeed to find solution.
Problem is:
I have a xml file named as "temp.xml" The text in the xml file is for example
<?xml version="1.0" encoding="utf-8"?>
<NoteData Note_Nbr="312" Data_Revision="2" Note_Revision="1" />
I want to save only Note_Nbr to my variable x (312)
I try a something but it doesn't work.
X=$(sed -r 's/[^|]*(Note_Nbr=")(\d\d\d)([^|]*)/2 /' temp.xml )
Thank you for your helps.
Upvotes: 0
Views: 344
Reputation: 295472
The right way to do this is with a real XML parser:
x=$(xmllint --xpath 'string(/NoteData/@Note_Nbr)' test.xml)
...or, if you have XMLStarlet rather than a new enough xmllint:
x=$(xmlstarlet sel -t -m '/NoteData' -v @Note_Nbr -n <test.xml)
See also this answer: https://stackoverflow.com/a/1732454/14122
Now, if you only wanted to work with literal strings, you could build something fragile that looked like this, using parameter expansion:
s='<NoteData Note_Nbr="312" Data_Revision="2" Note_Revision="1" />'
s=${s#* Note_Nbr=\"}; s=${s%%\"*}; echo "$s"
Alternately, you could use native regular expression support within bash (note that this functionality is a bash extension not present in POSIX sh):
s='<NoteData Note_Nbr="312" Data_Revision="2" Note_Revision="1" />'
re='Note_Nbr="([^"]+)"'
if [[ $s =~ $re ]]; then
match="${BASH_REMATCH[1]}"
else
echo "ERROR: No match found" >&2
fi
Upvotes: 1