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.
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.
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
.
x.x.x
Don't parse XML/HTML with regex, use a proper XML/HTML parser and a powerful xpath 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
python's lxml
(from lxml import etree
)
perl's XML::LibXML
, XML::XPath
, XML::Twig::XPath
, HTML::TreeBuilder::XPath
ruby nokogiri, check this example
php DOMXpath
, check this example