... * too greedy in sed
I want to extract the version number from multiple lines in bash without using too many additional packages. So far I've tried sed
.
Here is the API:
3.81-8.1ubuntu1.1 should give : 3.81
2.68-1ubuntu2 should give : 2.68
1:1.11.3-1ubuntu2 should give : 1.11.3
And here is my sed command:
echo ... | sed -r 's/.*([0-9\.]+).*/\1/'
However, the discovery is .*
too greedy, especially in the latter case. I have tried several .*?
and .\{-}
with no success.
I can do it in two passes, but I'd rather learn to do it in one.
+3
Offirmo
source
to share
2 answers
is it OK for you?
grep -Po "[\d\.]*(?=-)" file
Example:
kent$ cat tt
3.81-8.1ubuntu1.1
2.68-1ubuntu2
1:1.11.3-1ubuntu2
kent$ grep -Po "[\d\.]*(?=-)" tt
3.81
2.68
1.11.3
+8
Kent
source
to share
To overcome greed you need to be more strict with regex:
$ sed -r 's/.*([0-9]+\.[0-9]+\.?[0-9]+)-.*/\1/' file
3.81
2.68
1.11.3
This will match version numbers with major, minor, and build (optional), always followed by a hyphen.
+1
Chris seymour
source
to share