Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / June 2005

Tip: Looking for answers? Try searching our database.

How to obtain a version number from a String?

Thread view: 
kenshin@prontomail.com - 14 Jun 2005 23:01 GMT
Hi all,

I'm trying to find a way to obtain the version number from this String,
in this case, it should be "9":

Welcome to SUSE LINUX Enterprise Server 9 (i586) - Kernel \r (\l).

I was thinking about regular expression, match the pattern and somehow
get the version number.

I would really appreciate if any experts can give me a hand on this.
Thanks!
Remi Arntzen - 15 Jun 2005 00:06 GMT
It depends on if the String information changes structure.

If it says relatively the same you could just do this
String version = aString.substring(40).split(" ")[0];

There might be a better way though.

If the first number is always the version number then
String version = null;
String[] parts = aString.split(" ");
for (int i = 0; i < parts.length; i++) {
 try {
   Integer.parseInt(parts[i]);
 } catch (NumberFormatException thrown) {
   continue;
 }
 version = parts[i];
 break;
}

However that may be inefficient due to the use of looped try-catch
blocks, but should work.

Unfortunately, I am no expert with respect to regular expressions.
Shorty - 15 Jun 2005 07:58 GMT
Well you could use a regexp, provided you know what pattern to look
for. What would you say it is in this case ? can't be just an integer
(else 586 could be a version number).
So would it be an integer between spaces ? (what happens if you run
into a 9.3 version ?)
That is the first thing to know : how do you define the version number
so this definition can be applied to any string you might encounter and
enable to find the correct part.
Once you know that, building the regexp can be done (might be an ugly
one, but you can describe almost any pattern using regexp)
kenshin@prontomail.com - 15 Jun 2005 15:09 GMT
Thanks to both you for the replies, really appreciate it.

Anyway, these are the strings that I would expect (depends on which
Linux machine I run on):
Welcome to SUSE LINUX Enterprise Server 9 (i586) - Kernel \r (\l).
Welcome to SuSE SLES 8 (powered by UnitedLinux 1.0) (i586)
Red Hat Enterprise Linux AS release 3 (Taroon Update 3)
Turbolinux EnterpriseServer 8.0 (Quattro)
Red Flag Advanced Server release 4.1

Sometimes, the version is something like 9, and sometimes, is something
like 4.1
Seems like the pattern is kinda hard to construct :)
What do you think?
Eric Sosman - 15 Jun 2005 15:54 GMT
> Thanks to both you for the replies, really appreciate it.
>
[quoted text clipped - 10 lines]
> Seems like the pattern is kinda hard to construct :)
> What do you think?

   If you Really Know you're running on a Unix flavor,
you could try the `uname -r' command.

Signature

Eric.Sosman@sun.com

Gordon Beaton - 15 Jun 2005 16:08 GMT
> Anyway, these are the strings that I would expect (depends on which
> Linux machine I run on):
[quoted text clipped - 7 lines]
> Sometimes, the version is something like 9, and sometimes, is
> something like 4.1

Why do you expect a proper ordering between different vendor's product
version numbers?

If you want to know what *kernel* version is running, read Eric's
suggestion.

However if you really need to parse information out of the welcome
prompt, then I'd suggest changing the prompt so it contains the
information you need. Look at /etc/issue or /etc/issue.net for the
source of the strings you are trying to compare, and read the manpage
for e.g. mingetty.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

kenshin@prontomail.com - 15 Jun 2005 16:38 GMT
Thanks guys. I know 'uname -r' would return the kernel version, but I'm
looking for the actual OS version.

I'm writing a code to check if the machine has the matching version
from my meta-data file. The only way to obtain the OS version from a
Linux machine is through /etc/issue file.

Regular expression is one way to implement this, isn't it?

> > Anyway, these are the strings that I would expect (depends on which
> > Linux machine I run on):
[quoted text clipped - 25 lines]
> [  do not email me copies of your followups  ]
> g o r d o n + n e w s @  b a l d e r 1 3 . s e
HansF - 15 Jun 2005 17:04 GMT
On Wed, 15 Jun 2005 08:38:20 -0700, kenshin interested us by writing:

> I'm writing a code to check if the machine has the matching version
> from my meta-data file. The only way to obtain the OS version from a
> Linux machine is through /etc/issue file.

As long as you realize that /etc/issue is only initialized by the vendor,
and many SAs customize the contents.  (For example, one recommendation is
http://web.tampabay.rr.com/batcave/ServerBuilds.htm#Issue  ...)

If you want the distro's release info, check the files /etc/*-release (eg:

    /etc/redhat-release
    /etc/debian_version
    /etc/SuSE-release
    /etc/slackware-version
    /etc/gentoo-release
    /etc/cobalt-release

Again, not entirely consistent, but possibly closer to what you want.  

(Still not sure what you define as 'OS', though.)

Signature

Hans Forbrich                          

Andrew Thompson - 15 Jun 2005 17:26 GMT
> ..The only way to obtain the OS version ..

As in <http://www.physci.org/pc/property.jsp?prop=os.version>

>..from a Linux machine is through /etc/issue file.

?

Signature

Andrew Thompson
http://www.PhySci.org/codes/  Web & IT Help
http://www.PhySci.org/  Open-source software suite
http://www.1point1C.org/  Science & Technology
http://www.LensEscapes.com/  Images that escape the mundane

Gordon Beaton - 15 Jun 2005 17:40 GMT
> Thanks guys. I know 'uname -r' would return the kernel version, but I'm
> looking for the actual OS version.
>
> I'm writing a code to check if the machine has the matching version
> from my meta-data file.

Then why don't you just check if the whole string matches?

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

kenshin@prontomail.com - 15 Jun 2005 19:30 GMT
Thank you all for the valuable feedbacks and suggestions! :)

After experimenting a bit with regular expressions, this seems to do
the trick:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OSTest
{
        public static void main (String[] args)
        {
                String sIssue = "Red Hat 88 (Update 3456)";
                String sRegex = "\\d+[.]\\d+|\\d+";
                System.out.println (parseIssueFileForVersionNumber( sIssue, sRegex
));
        }

        private static String parseIssueFileForVersionNumber ( String sIn,
                String sPattern)
            {
                Pattern patternLanguage = Pattern.compile( sPattern );
                Matcher matcher = patternLanguage.matcher( sIn );

                   matcher.find();
                int nStart = matcher.start();
                int nStop = matcher.end();
                sIn = sIn.substring(  nStart,  nStop);               
                return sIn;
            }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.