Reputation: 3
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*'
Upvotes: 0
Views: 418
Reputation:
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\}'
Upvotes: 1
Reputation: 10039
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).
Upvotes: 0
Reputation: 2376
This passes both tests
egrep -o '[0-9]\S*'
Unfortunately, not all greps support -o
, but grep in Linux does.
Upvotes: 0
Reputation: 103694
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
Upvotes: 1
Reputation: 13640
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'
Upvotes: 0
Reputation: 14490
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
Upvotes: 0