mulrus
mulrus

Reputation: 85

Value parsing from XML tag

cat /home/user/my.xml | grep -e productVersion | awk {'print $2'}

It parses data from file my.xml where I have a tag such like this:

<product productVersion="10.2.14.0" Type="test" Sub="0.3">

and finally I have a good result:

productVersion="10.2.14.0"

But on any oldest versions XML at others desktops, column can be not $2 and $3, and $4 etc. Of cause I tried to do ID ELSE, it works but I really couldn't know each column on each PC.

May be is it some function or trick for example where I can put productVersion=* and where I can see result from tag productVersion="10.2.14.0"?

Upvotes: 0

Views: 48

Answers (2)

Ed Morton
Ed Morton

Reputation: 203664

Since you asked for an awk answer:

$ awk 'match($0,/productVersion="[^"]+"/){ print substr($0,RSTART,RLENGTH) }' file
productVersion="10.2.14.0"

or in gawk:

$ awk 'match($0,/productVersion="[^"]+"/,a){ print a[0] }' file
productVersion="10.2.14.0"

Note that the ticks go OUTSIDE of the awk script (awk '<whole script>'), not inside the curly brackets awk {'<rest of script>'}), since the ticks are the script delimiters and the curly brackets are just part of the script.

Upvotes: 0

choroba
choroba

Reputation: 241918

Use a proper parser to extract information from the XML.

xmllint my.xml --xpath '//product/@productVersion'

Upvotes: 2

Related Questions