LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-23-2011, 04:28 PM   #1
cosmic_cow
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Rep: Reputation: Disabled
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
 
Old 08-23-2011, 04:54 PM   #2
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
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/"
 
Old 08-23-2011, 07:10 PM   #3
cosmic_cow
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
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).
 
Old 08-24-2011, 02:38 PM   #4
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,091

Rep: Reputation: 41
Quote:
Originally Posted by cosmic_cow View Post
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.
 
1 members found this post helpful.
Old 08-25-2011, 10:17 AM   #5
cosmic_cow
LQ Newbie
 
Registered: Aug 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by trey85stang View Post
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
 
Old 08-25-2011, 11:15 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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]}"
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing version numbers MTK358 General 2 07-16-2010 04:27 PM
Graphical dialog from bash without grabbing focus catkin Programming 3 06-29-2009 03:01 PM
Bash command to list installed libraries and version numbers newtovanilla Linux - Newbie 4 07-18-2008 04:49 PM
Simple bash script help, grabbing part of a string colabus Linux - Newbie 3 04-25-2005 09:42 AM
Version numbers X11 Programming 2 04-09-2002 08:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration