0

I have a string which has below value

String1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64

I need to extract only version from this string which is "1.2.0-4"

I have tried regular expression as mentioned below with sed

sed -ne 's/[^0-9]*\(\([0-9]\.\)\{0,1\}[0-9][^.]\).*/\1/p'

but I am only getting result "1.2.0-", missing number after "-" which is 4. I tried correcting it but it is returning null.

Kindly, advise

5 Answers 5

1

how about grep:

grep -Po "(?<=-)[\d.-]*(?=.\d{8})"
Sign up to request clarification or add additional context in comments.

Comments

1

This should work, and should also account for any of the version numbers being more than one digit long.

String1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64
sed -n 's/[^0-9]*\(\([0-9]\+\.\)\{0,2\}[0-9]\+-[0-9]\+\).*/\1/p' <<< "$String1"
1.2.0-4

BTW, this will also match version strings like (which from your question is not clear if you want this behavior or not):

1-1
1.2-1

If you want to enforce w.x.y-z, you could use this:

sed -n 's/[^0-9]*\(\([0-9]\+\.\)\{2\}[0-9]\+-[0-9]\+\).*/\1/p' <<< "$String1"

1 Comment

Thank You ! very much. Could you also suggest a reg ex for checking versions What if I like to check any version in 1.0.0 release may be "1.0.0-1" or "1.0.0-2" or "1.0.0-3" and I just need to check for what update version is it "-1 or -2 or -3" "1.0.0-1 or 1.0.0-2 or 1.0.0-3" what regex can I use ? "sed -ne s/1.0.0\-[0-9]\1/p" would this work ? Thanks ! sed -n 's/1.0.0\-[0-9]'
1
sed -n 's/^.*-\([0-9.]*-[0-9]*\)\..*$/\1/p'

Comments

0

Can you try this?

[^0-9]*\(\([0-9]\.\)\{0,2\}[0-9][^.]\).*

Comments

0

You could also try parameter expansion:

string1=winos-app-1.2.0-4.20120308.InLinuxOS.x86_64
string2=${string1%".${string1#*-*-*-*.}"}
version=${string2#*-*-}
printf "%s\n" "$version"

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.