LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash: Grabbing version numbers (https://www.linuxquestions.org/questions/linux-general-1/bash-grabbing-version-numbers-899026/)

cosmic_cow 08-23-2011 04:28 PM

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

trey85stang 08-23-2011 04:54 PM

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/"

cosmic_cow 08-23-2011 07:10 PM

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).

trey85stang 08-24-2011 02:38 PM

Quote:

Originally Posted by cosmic_cow (Post 4451545)
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).


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.

cosmic_cow 08-25-2011 10:17 AM

Quote:

Originally Posted by trey85stang (Post 4452419)
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.

Perfect. The extension won't ever be anything other than .jar in this case. Thanks very much.
Colin

David the H. 08-25-2011 11:15 AM

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'

re='-([[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+)\.'

[[ "$string" =~ $re ]]

echo "${BASH_REMATCH[1]}"

It's best to store the regex in a separate variable, to avoid having to escape special characters. The one I chose above looks for combinations of -number.number.number., and captures everything except the first dash and last period (the full match is stored in ${BASH_REMATCH[0]}, BTW). If any of the version numbers could have non-numeric characters then you'll have to change the character classes to follow suit, i.e. use alnum instead of digit.

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'

re='-(([[:digit:]]+)\.([[:digit:]]+)\.([[:digit:]]+))\.'

[[ "$string" =~ $re ]]

echo "${BASH_REMATCH[1]}"
echo "${BASH_REMATCH[2]}"
echo "${BASH_REMATCH[3]}"
echo "${BASH_REMATCH[4]}"



All times are GMT -5. The time now is 08:06 AM.