bash: Grabbing version numbers
I'm working on a script wherein I'll be comparing some version numbers using a MAJOR.MINOR.HOTFIX style system. Something like this-program-2.4.13.jar. It's easy enough to use awk to break it up at the hypens, use substring manipulations to get rid of the extension (it'll always be the same) and use a while loop until I get to the bit with the numbers, but I'm not sure what to do to verify that I'm actually at the point when I get there. Like in the example above, I could awk it down to 'this', 'program', and '2.4.13' easily enough and use some sort of while loop structure to go through and look at each one, but what sort of regex would I do to verify that echo $THING | awk '{ print $2 }' was giving me that version number?
I seriously hope that makes sense. Thanks, Colin |
best way to ask these questions is provide before and after data examples. ALl I get is that you want a script extract the so called version number from some kind of file name.
this-program-2.4.13.jar to 2.4.13 The biggest question is are all of your files in that same naming format? Code:
echo "this-program-2.4.13.jar" | sed "s/.*-\(.*\)\.[a-zA-Z0-9]\{3\}$/\1/" |
Ah, I suppose I wasn't very clear. That's exactly what I need (the version number extraction). Everything I'm going to be doing with the version I know how to do on my own. They will all be in the same format, in that it will be basically NAME-##.##.##.jar. The NAME can contain hyphens, as well (i.e. THIS-IS-THE-HYPOTHETICAL-FILENAME-##.##.##.jar) or it could contain none, other than what's separating the name and version (i.e. FILENAME-##.##.##.jar).
|
Quote:
Awesome, the line I gave you makes assumptions though; there version number sequence will only come before a three character file ending... i.e. .jar .txt .999 .gz9, it will not work correctly if you have a file ending like .tar.gz or .gz or .file The regex can be changed to match those optional file endings but mine does not account for it. Other then that I think that sed line will do what you want. |
Quote:
Colin |
Bash can do regex extraction too by the way. Just get the string into a variable first, then run [[ regex test on it. The BASH_REMATCH array will hold a successful match and any parenthetical groupings.
Code:
string='this-program-2.4.13.jar' You could even separate out the three subsections at the same time simply by adding a few more parentheses. Code:
string='this-program-2.4.13.jar' |
All times are GMT -5. The time now is 08:06 AM. |