FG
💻 Software

How to extract a version number using sed?

Fresh5 days ago
Mar 15, 202632923 views
Confidence Score0%
0%

Problem

I'm trying to find the best regular expression to extract a version number from a string. For example: The problem here is that it works only if the version number is in the format "a.b.c.d". If someone decides to add or remove a digit ("a.b.c.d.e", "a.b.c"), it will break. So I'd like to factorize…

Error Output

echo "Version 1.2.4.1 (release mode)" | sed -ne 's/.*\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p'

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How to extract a version number using sed?

Low Risk

Try next 'sed' command: It uses the {i,j} syntax, which selects the expression a number of times between the first and the last number. There souldn't be any numbers in the string before the version number. Another examples: EDIT to comments:

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment