Home > Archive > PERL Miscellaneous > March 2006 > How do i get the charactors inside the ( ) from a string variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
How do i get the charactors inside the ( ) from a string variable
|
|
| infinity12star@gmail.com 2006-03-25, 7:00 pm |
| Hi all
i've got a string as follow:
$string = " Linux version 2.6.13-15-default (g o@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.
| |
| Josef Möllers 2006-03-25, 7:00 pm |
| infinity12star@gmail.com wrote:
> Hi all
>
> i've got a string as follow:
> $string = " Linux version 2.6.13-15-default (g o@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 "g o@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
--
josef punkt moellers bei gmx punkt de
| |
| Eric Schwartz 2006-03-27, 7:00 pm |
| infinity12star@gmail.com writes:
> 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
|
|
|
|
|