Reputation: 27
Guys i have a string like this:
variable='<partyRoleId>12345</partyRoleId>'
what i want is to extract the value so the output is 12345.
Note the tag can be in any form:
<partyRoleId>
or <ns1:partyRoleId>
any idea how to get the tag value using grep or sed only?
Upvotes: 0
Views: 49
Reputation: 1428
to use only grep, you need regexp to find first closing brackets and cut all digits:
echo '<partyRoleId>12345</partyRoleId>'|grep -Po ">\K\d*"
-P means PCRE -o tells to grep to show only matched pattern and special \K tells to grep cut off everything before this.
Upvotes: 0
Reputation: 241798
Use an XML parser to extract the value:
echo "$variable" | xmllint -xpath '*/text()' -
You probably should use it for the whole XML document instead of extracting a single line from it into a variable, anyway.
Upvotes: 2