0

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?

  • [Don't Parse XML/HTML With Regex.](https://stackoverflow.com/a/1732454/3776858) I suggest to use an XML/HTML parser (xmlstarlet, xmllint ...). – Cyrus Apr 04 '20 at 20:32

2 Answers2

2

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.

choroba
  • 231,213
  • 25
  • 204
  • 289
0

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.

Saboteur
  • 1,331
  • 5
  • 12