user55691
user55691

Reputation: 113

grep exact match in colon delimited string

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

Answers (2)

n2studio
n2studio

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

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions