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?

1

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i wqnt to used grep or sed only
@yarabyarab: These tools are not good for parsing XML. Do you know that the element can also be equivalently written as <partyRoleId><![CDATA[12345]]></partyRoleId>?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.