fa__
fa__

Reputation: 327

Get variable version number

I need to extract version number for a version output like the following:

    Program updated version 7.9
    Released on 04-04-2013

I know how to extract a two-digit version number:

   grep -Po '(version )\d+(?:\.\d+){2}')

Or any other number of digits.The problem is that the version number may have different number of digits. Is there a regex I can user for a group of several digits separated by dots provided that the number of digits is unknown?

Upvotes: 0

Views: 99

Answers (1)

Jayesh Bhoi
Jayesh Bhoi

Reputation: 25865

With grep

#!/bin/sh
version=$(echo "Program updated version 7.9.9.9" | grep -oP "(?<=version )[^ ]+")
echo $version

Output:

$ ./test.sh
7.9.9.9

with awk

#!/bin/sh
version=$(echo "Program updated version 7.9.9.9" | awk '{for(i=1;i<=NF;i++) if ($i=="version") print $(i+1)}')
echo $version

Output:

$ ./test.sh
7.9.9.9

Upvotes: 1

Related Questions