-3

INPUT- cat my.txt

The version of the current file

<version>x.x.x-SNAPSHOT</version> 

Desired output:

x.x.x

(which are digits and dynamic values)

Tried multiple grep and awk commands but no luck.

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223

1 Answers1

1

Like this:

xmllint --xpath '
    substring-before(//*[contains(text(), "-SNAPSHOT")]/text(), "-SNAPSHOT")
' file.xml

From a pipe:

curl -s 'http://example.com/query_string' |
    xmllint --xpath '
        substring-before(//*[contains(text(), "-SNAPSHOT")]/text(), "-SNAPSHOT")
' -

You can replace trailing - by /dev/stdin.

Output

x.x.x

Note

Don't parse XML/HTML with regex, use a proper XML/HTML parser and a powerful query.

You can use one of the following :

xmllint often installed by default with libxml2-utils, xpath1

xmlstarlet can edit, select, transform... Not installed by default, xpath1

xpath installed via perl's module XML::XPath, xpath1

xidel xpath3

saxon-lint my own project, wrapper over @Michael Kay's Saxon-HE Java library, xpath3

or you can use high level languages and proper libs, I think of :

's lxml (from lxml import etree)

's XML::LibXML, XML::XPath, XML::Twig::XPath, HTML::TreeBuilder::XPath

, check this example

DOMXpath, check this example


Check: Using regular expressions with HTML tags

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223