I have a string with components and version numbers:
data-c(kuh-small1);divider-bin-1.4.4;divider-conf-1.3.3-w(1,16);storage-bin-1.5.4;storage-conf-1.5.0-w(1);worker-bin-4.5.1;worker-conf-4.4.1-c(kuh)-win2
For a shell script, I need to extract the version number of the divider binary. So I need to yield:
1.4.4
What would be a good way to do this? with sed?
You can also use perl:
sed can handle this easily....
There are a few other things you can do to tighten it up... such as stop grabbing the version number when you reach the ";"
This general answer should work in all cases
I use one of these three one-line perl commands depending on the expected input string:
The input string always contain one single version
To extract several versions (several lines)
To extract one single version (the first one) in all cases
1. The simplest
The first command line do not embed the final newline:
Store the version within a shell variable:
But fails for multi version numbers:
2. Extract one version per line
3. Extract the first version only
Create an alias:
or if you use a recent bash:
infos is between
;divider-bin-
and next;
and start with a digit. This is to ensur that noOther-divider-bin-
ordivider-bin-and-hex-
will interfere with your request. Also, return empty string if not find. to be exhaustif (assuming version is only digit and dot)Following Kent's answers, this can work:
and even better:
it greps from
divider-bin-
until it find the;
character. This way anyNNN.NNN. ... . NNN
format will work (no matter how many blocks ofNN
).Test: