Reputation: 113
I am trying to extract the version from a colon delimited list. The value I want is for foo, however there is another value in the list called foo-bar causing both values to return. This is what I am doing:
LIST="foo:1.0.0
foo-bar:1.0.1"
VERSION=$(echo "${LIST}" | grep "\bfoo\b" | cut -s -d':' -f2)
echo -e "VERSION: ${VERSION}"
Output:
VERSION: 1.0.0
1.0.1
NOTE: Sometimes LIST will look like the following, which should result in version being empty (this is expected).
LIST="foo
foo-bar:1.0.1"
Upvotes: 1
Views: 790
Reputation: 306
This regex should extract any number and optional dots at the end of each line. If the line ends with a colon, then it won't match.
grep -oE '(([[:digit:]]+[.]*)+)$
Upvotes: 0
Reputation: 627219
You may use a PCRE regex enabled with -P
option and use a (?!-)
negative lookahead that will fail the match in case there is a -
after a whole word foo
:
grep -P "\bfoo\b(?!-)"
See online demo
Upvotes: 1