How do i get the charactors inside the ( ) from a string variable

I

infinity12star

Hi all

i've got a string as follow:
$string = " Linux version 2.6.13-15-default (geeko@buildhost) (gcc
version 4.0.2 20050901 (prerelease) (SUSE Linux) ) #1 Tue Sep 13
14:56:15 UTC 2005"

how do i only get the "SUSE Linux" inside the ( ) ??

do i look for (** ( SUSE Linux) ) ?

i can't jst hard code it to get SUSE Linux, because the output differs
from time to time

can someone plz help me thx.
 
J

Josef Möllers

Hi all

i've got a string as follow:
$string = " Linux version 2.6.13-15-default (geeko@buildhost) (gcc
version 4.0.2 20050901 (prerelease) (SUSE Linux) ) #1 Tue Sep 13
14:56:15 UTC 2005"

how do i only get the "SUSE Linux" inside the ( ) ??

do i look for (** ( SUSE Linux) ) ?

i can't jst hard code it to get SUSE Linux, because the output differs
from time to time

can someone plz help me thx.

How do you know it's not the "geeko@buildhost" or the "prerelease" you're
after?
Specify exactly how to determine what you want and you may be able to
construct the RE for that, e.g. "it's the second pair of parentheses which
are within a set of parentheses":
\([^)]*\([^)]+\).*\(([^)]+)\)

Josef
 
P

Peter J. Holzer

i've got a string as follow:
$string = " Linux version 2.6.13-15-default (geeko@buildhost) (gcc
version 4.0.2 20050901 (prerelease) (SUSE Linux) ) #1 Tue Sep 13
14:56:15 UTC 2005"

how do i only get the "SUSE Linux" inside the ( ) ??

do i look for (** ( SUSE Linux) ) ?

i can't jst hard code it to get SUSE Linux, because the output differs
from time to time

can someone plz help me thx.

No. If the string differs from time to time, and you post only one
string, how are we supposed to know how it differs?

Try to find out what changes and stays the same. For example, if you
find that the string you are interested in is always in the last nested
pair of parentheses, you might use:

if ($string =~ m/\(.*\((.*)\) \)/) {
$distro = $1;
}

hp
 
I

infinity12star

Hi all

i've got a string as follow:
$string = " Linux version 2.6.13-15-default (geeko@buildhost) (gcc
version 4.0.2 20050901 (prerelease) (SUSE Linux) ) #1 Tue Sep 13
14:56:15 UTC 2005"

how do i only get the "SUSE Linux" inside the ( ) ??

do i look for (** ( SUSE Linux) ) ?

i can't jst hard code it to get SUSE Linux, because the output differs
from time to time

can someone plz help me thx.

OK the string inside the ( ) differs only when the OS installed is
differ, e.g. ( Red Hat Linux )
so i need to get the version of the OS and print it out as a output,
can you specify what should i do with it, because i'm not good in
filtering the strings :(
 
E

Eric Schwartz

OK the string inside the ( ) differs only when the OS installed is
differ, e.g. ( Red Hat Linux )
so i need to get the version of the OS and print it out as a output,
can you specify what should i do with it, because i'm not good in
filtering the strings :(

Make your life much easier-- look at /etc/redhat-release and
/etc/SuSE-version instead:

if (-f /etc/redhat-release) {
print "We're running Red Hat, version ";
my $version = `cat /etc/redhat-release`;
print $version, "\n";
} elsif (-f /etc/SuSE-version) {
print "We're running SuSE, version ";
my $version = `cat /etc/SuSE-version`;
print $version, "\n";
}

For extra credit, figure out what file is used for debian, CentOS,
Ubuntu, etc.

Note: Yes, I could have used File::Slurp or some such instead; I
didn't really want to clutter the example with it.

-=Eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top