edoreld
edoreld

Reputation: 301

How to use sed to extract a matching pattern from a text file?

I am trying to extract the element contained in version by using sed. I have a pattern that matches all elements like it ([0-9]{1,2}\.[0-9]{1}\.[0-9]{1}), but I just need the first match.

The objective would be to be able to output the match, and just the match - without the quotes - to standard output.

Any ideas on how I could do this?

<?xml version='1.0' encoding='utf-8'?> <widget id="com.company.application" version="1.0.0" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0"> ...

I h

Upvotes: 1

Views: 188

Answers (2)

Pedro Lobito
Pedro Lobito

Reputation: 99011

Don't use regex to parse xml, use a xml parser, xmllint for example, which is bundled on most linux/mac, i.e.:

xmllint --xpath 'string(//*[local-name()="widget"]/@version)' xmlfile
# 1.0.0

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92894

The solution using xmlstarlet tool:

xmlstarlet sel -t -m '_:widget' -v "@version" -n xmlfile

The output:

1.0.0

-v "@version" - get value of version attribute

Upvotes: 0

Related Questions