You can use perl in sed-like replacement syntax and achieve whatever you need using Perl Regexp Grouping:
$ echo "$a"
"7.6.0-custom-build"
$ perl -pe 's/(^.)(.*?)-(.*?)(.$)/\1\2\4/' <<<"$a"
"7.6.0"
#Input string is divided in groups :
#1st group contains the first character " or ' or whatever it is.
#2nd group match any char for zero or more times in use with the "non-greedy operator `?`"
#4th group match the end of line with the previous char (either " or ' or whatever
#The whole input string is replaced by group 1-2-4
$ perl -pe 's/(^.)(.*?)-(.*?)(.$)/\2/' <<<"$a"
7.6.0
# Similarly , without the 1st and last chars = only group2
$ perl -pe 's/(^.)(.*?)-(.*?)(.$)/\2/;s/[.-]//g' <<<"$a"
760
#Like before but with an extra substitution to remove literal dots or dashes that use to exist in version.
With the last command you can convert any string representing a version number to a flat number :
7.6.0
to 760
7.1.0
to 710
Thus you can directly compare them as integers with bash -gt
/ -lt
operators.
Including dash in the last example you can also transform versions like :
7.6.1-1
to 7611
.
PS: I prefer perl -pe
over sed
because:
(a) perl supports non-greedy regexp while sed does not support them
(b) serial substitutions of perl refer to the previously transformed text , while on sed each substitution refers to the initial - incoming text/stream
(c) Perl will work on the same way in all the systems that they do have perl installed (i.e Debian, BSD, MAC, wherever) and that makes it fully portable.