thclpr
thclpr

Reputation: 5938

Print text value from xml using sed

using a one line sed command , how could i get the something and someone value?

<value name="something">someone</value>

Using the following regex <value name="(.*)">(.*)<\/value> i could retrieve the values with success using the site https://www.regex101.com/. But i'm not sure how could i do it using the command line.

Thanks in advance.

Upvotes: 0

Views: 58

Answers (3)

glenn jackman
glenn jackman

Reputation: 246764

Noting the usual caveats about parsing XML with regular expressions, here's an XML parsing tool in action on your sample data that finds the attribute value and tag value for a "value" tag with a "name" attribute.

xmlstarlet sel -t -v '//value[@name]/@name' -n -v '//value[@name]' -n file.xml
something
someone

Upvotes: 0

Karthikeyan.R.S
Karthikeyan.R.S

Reputation: 4041

Try this.

sed -r 's/.*"(.*)">(.*)<.*>$/\1 \2/'

Upvotes: 0

nu11p01n73R
nu11p01n73R

Reputation: 26667

Something like

sed 's#.*name="\(.*\)">\(.*\)<.*#\1 \2#g'

Test

$ echo "<value name=\"something\">someone</value>" | sed 's#.*name="\(.*\)">\(.*\)<.*#\1 \2#g'
something someone

Upvotes: 1

Related Questions