0

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?

5
  • 1
    \d[\d\.]* – regex for a string starting with a number and containing just digits and points Commented Apr 7, 2014 at 8:34
  • @tampis I have just tried your suggestion and i still get only 2 digits. I don't know if i am doing sth wrong. I use version=$(echo $output | grep -Po '\d[\d\.]*'). Commented Apr 7, 2014 at 8:59
  • Thank you @Jayesh. The colum is not fixed. I have tested your solution and i don't know why but it sorts the numbers in pairs. For example for "Program updated version 7.9" the output is 7.9 For "Program updated version 7.9.9" the output is still 7.9 and for "Program updated version 7.9.9.9" the output is 7.9 9.9 with a space between the pairs Commented Apr 7, 2014 at 9:04
  • @fa__ look my answer. Commented Apr 7, 2014 at 9:10
  • This worked to me: grep -Po '(?<=version )[0-9.]+' file does it suffice or you want other conditions? Commented Apr 7, 2014 at 9:28

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

4 Comments

With both this and @fedorqui's suggestion I experience the problem i described above of splitting in pairs.Is it only me with that issue?
@fa__ if you provide space like 7.9 9.9 then obviously you get 7.9. i don't understand you what you mean?
The problem is i don't provide that space. I write 7.9.9.9.
@fa__ ok so it display 7.9.9.9 as per my solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.