0

I use sed in CentOs to extract version number and it's work fine:

echo "var/opt/test/war/test-webapp-4.1.56.war" | sed -nre 's/^[^0-9]*(([0-9]+\.)*[0-9]+).*/\1/p'

But my problem is that i am not able to extract when the version is shown like this:

var/opt/test/war/test-webapp-4.1.56-RC1.war

I want to extract the 4.1.56-RC1 if it is present.

Any ideas ?

EDIT 2

Ok to be clear take this example, with a path:

Sometimes the path contains only a serial number like this var/opt/test/war/test-webapp-4.1.56.war and sometimes it contains a series of numbers and letters like this "var/opt/test/war/test-webapp-4.1.56-RC1.war

The need is to recover either 4.1.56 or 4.1.56-RC1 depending on the version present in the path. With sed or grep, no preference.

This seems to work but the .war is shown at the end:

echo "var/opt/test/war/test-webapp-4.1.56.war" | egrep -o '[0-9]\S*'
7
  • why don't you just say echo "version ..." | awk '{print $2}'? Commented May 5, 2015 at 19:37
  • because maybe the extract is make from a path like this: /var/opt/test/war/test-webapp-4.1.56-RC1.war Commented May 5, 2015 at 19:38
  • 2
    update your question with the full input. Commented May 5, 2015 at 19:40
  • 1
    I'm going to vote close based "Unclear what you're asking" This question will be of no help to future users if you don't update it. Commented May 5, 2015 at 19:44
  • 2
    @Marc wrt because maybe the extract is make from a path like this: /var/opt/test/war/test-webapp-4.1.56-RC1.war - so what if it was? Don't answer that in a comment but instead edit your question to show examples of all possible version info formats that you want to be able to handle along with the expected output for each. Commented May 5, 2015 at 19:50

6 Answers 6

1

Little unclear what you are after, but this seems to be in the general direction.

Given:

$ echo "$e"
/var/opt/test/war/test-webapp-4.1.56-RC1.war
/var/opt/test/war/test-webapp-RC1.war
Version 4.2.4 (test version)

Try:

$ echo "$e" | egrep -o '(\d+\.\d+\.\d+-?\w*)'
4.1.56-RC1
4.2.4
Sign up to request clarification or add additional context in comments.

Comments

1

The following will match the first digit up to 2 digits in length ({1,2}, second up to 2 digits and the last up to 4 digits followed by anything non-space up to a space.

grep -o '[0-9]\{1,2\}.[0-9]\{1,2\}.[0-9]\{1,4\}' 

Comments

0

Just add (-[a-zA-Z]+[0-9]+) to your regex:

echo "Version 4.2.4 (test version)" | sed -nre 's/^[^0-9]*(([0-9]+\.)*[0-9]+(-[a-zA-Z]+[0-9]+)).*/\1/p'

1 Comment

Thanks. I used your code and I modified as follows for my needs: echo "/var/opt/test/war/test-webapp-4.1.56-RC1.war" | sed -nre 's/^[^0-9]*(([0-9]+\.)*[0-9]+(-[a-zA-Z]*[0-9]*[a-zA-Z]*)*).*/\1/p'
0

What about just using whitespace as the delimiter like

echo "Version 4.2.4-RC1 (test version)" | grep -Po "Version\s+\K\S+"

for grep -P says to use Perl style regex, -o shows only the matching part and the \K in the string says not to show everything before it as part of the match

2 Comments

it's good idea but maybe the extract is make from a path like this: /var/opt/test/war/test-webapp-4.1.56-RC1.war. It's not the same format each timee
In that case, is it reliable that it's always the 3rd and alter in the last part? Could you do something like basename <path> | cut -f 3- -d- | cut -f1 -d.
0

This passes both tests

egrep -o '[0-9]\S*'

Unfortunately, not all greps support -o, but grep in Linux does.

Comments

0
echo "Version 4.2.4 (test version)" | sed 's/Version[[:space:]]*\([^[:space:](]*\).*/\1/'

But like every extraction, you need to define what you want, not what could exist and extract it (or change your request).

Comments

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.