Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

I have done this before

by monk2b (Pilgrim)
on Jul 13, 2007 at 18:01 UTC ( #626499=perlquestion: print w/replies, xml ) Need Help??

monk2b has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, It must be true for me, if you don't use it you lose it. I am trying to find out the hostname of systems that are using this certain software, by querying an application from the program in question. The output looks like the Information below the __DATA__ tag. I am able to get this to work using split but I can not get this to work with a regular expression. My reason for wanting to use a regex is to keep errors down when I have blank lines or lines like this one.(example "--------" ) The excessive use of print is for debugging purposes. This is the code that I have so far.
#!/usr/bin/perl use strict; use warnings; while( <DATA> ) { print "$_"; while ( $_ =~ /(\w+)\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+\s+\w+/g ) { print "Using regex Host is $1\n"; } my ($host, $arch, $proc, $load, $memtot, $memuse, $swap, $swapuse ) += split( " ", $_); if ( $host =~ /\w+/) { print "Using split function Host is $host\n"; } # if ( $arch =~ /\w+/) { # print "Arch is $arch\n"; # } # if ( $proc =~ /\w+/) { # print "Num of processors is $proc\n"; # } # if ( $load =~ /\w+/) { # print "Load is $load\n"; # } # if ( $memtot =~ /\w+/) { # print "Total memory is $memtot\n"; # } # if ( $memuse =~ /\w+/) { # print "Memory used is $memuse\n"; # } # if ( $swap =~ /\w+/) { # print "Total swap is $swap\n"; # } # if ( $swapuse =~ /\w+/) { # print "Swap used is $swapuse\n"; # } } __DATA__ HOSTNAME ARCH NPROC LOAD MEMTOT MEMUSE SWAPTO + SWAPUS ---------------------------------------------------------------------- +--------- global - - - - - - + - Luke glinux 2 0.70 3.4G 919.5M 1.9G + 681.6M bobafett glinux 2 0.00 3.4G 205.8M 1.9G + 0.0 bones glinux 2 0.01 2.0G 150.9M 1.9G + 0.0 c3p0 glinux 2 0.37 3.4G 101.6M 1.9G + 7.2M chewbacca glinux 4 0.00 2.0G 36.6M 1.9G + 0.0 crusher glinux 2 0.01 2.0G 250.8M 4.0G + 0.0 data glinux 4 0.01 2.0G 586.9M 1.9G + 230.8M dax glinux 4 0.81 2.0G 438.0M 1.9G + 272.1M geordi glinux 4 - 2.0G - 1.9G + - han glinux 2 0.53 3.4G 373.6M 1.9G + 243.3M janeway glinux 2 0.00 7.3G 351.9M 16.0G + 0.0 lando glinux 4 1.27 7.3G 445.8M 1.9G + 6.9M nelix glinux 2 - 2.0G - 1.9G + - odo glinux 2 - 2.0G - 15.6G + - picard glinux 2 - 2.0G - 1.9G + - quark glinux 2 0.24 3.4G 177.7M 1.9G + 14.6M r2d2 glinux 2 1.09 3.4G 109.2M 1.9G + 7.0M scottie glinux 2 0.00 2.0G 151.4M 1.9G + 0.0 sisko glinux 2 - 2.0G - 1.9G + - sulu glinux 2 - 493.7M - 1019.7M + -


I got 99 problems, and right now this is number one.

Replies are listed 'Best First'.
Re: I have done this before
by saintly (Scribe) on Jul 13, 2007 at 18:43 UTC
    '\w' doesn't match the '.' or '-' characters, so the regex fails. It only matches A-Z, a-z, 0-9 and the underscore. If you want to go this route, change at least the 3-8th '\w's to something like [A-Z0-9\.\-]. For even more security and readability, you can construct your regex in a couple extra stages:
    # prepare regex sub-expressions my $sizerx = '(-|\d+(\.\d+)?[GM]?)'; # Matches '-', or a float + 'M' + or 'G' for size my $loadrx = '(-|\d+\.\d\d)'; # Matches '-' or a float my $procrx = '(-|\d+)'; # matches '-' or an integer # Test the line to see if it matches if( $_ =~ /^(\w+)\s+ # host (\w+)\s+ # os $procrx\s+ # nproc $loadrx\s+ # load $sizerx\s+ # memtot $sizerx\s+ # memuse $sizerx # swapto \s*$ /x ) { print "using regex, host is [$1]\n"; }
    For speed, you should define the sub-expressions outside the loop. If you plan to work with the various parts, you could immediately assign the matches to named variables and check for them, like so:
    my($host,$os,$nproc,$load,$memtot,$memuse,$swapto) = ( $_ =~ /^(\w+)\s+ # host (\w+)\s+ # os $procrx\s+ # nproc $loadrx\s+ # load $sizerx\s+ # memtot $sizerx\s+ # memuse $sizerx # swapto \s*$ /x ); if(defined $host) { print "using regex, host is [$host]\n"; } else { next; }
Re: I have done this before
by toolic (Bishop) on Jul 13, 2007 at 18:42 UTC
    I added a blank line to your data, and removed some data lines for simplicity. I know you said you wanted to use regex instead of split, but I do not understand why. Is this what you are trying to achieve?
    #!/usr/bin/env perl use strict; use warnings; while ( <DATA> ) { next if /^HOSTNAME/; # skip title line next if /^\-/; # skip lines which start with "-" s/^\s+//; # remove whitespace at start of line s/\s+$//; # remove whitespace at end of line next if /^$/; # skip blank lines my ($host, $arch, $proc, $load, $memtot, $memuse, $swap, $swapuse +) = split; print "Using split function Host is $host\n"; } __DATA__ HOSTNAME ARCH NPROC LOAD MEMTOT MEMUSE SWAPTO + SWAPUS ---------------------------------------------------------------------- +--------- global - - - - - - + - Luke glinux 2 0.70 3.4G 919.5M 1.9G + 681.6M bobafett glinux 2 0.00 3.4G 205.8M 1.9G + 0.0 bones glinux 2 0.01 2.0G 150.9M 1.9G + 0.0 c3p0 glinux 2 0.37 3.4G 101.6M 1.9G + 7.2M dax glinux 4 0.81 2.0G 438.0M 1.9G + 272.1M geordi glinux 4 - 2.0G - 1.9G + - han glinux 2 0.53 3.4G 373.6M 1.9G + 243.3M janeway glinux 2 0.00 7.3G 351.9M 16.0G + 0.0 lando glinux 4 1.27 7.3G 445.8M 1.9G + 6.9M sisko glinux 2 - 2.0G - 1.9G + - sulu glinux 2 - 493.7M - 1019.7M + -
    gives me this output:
    Using split function Host is global Using split function Host is Luke Using split function Host is bobafett Using split function Host is bones Using split function Host is c3p0 Using split function Host is dax Using split function Host is geordi Using split function Host is han Using split function Host is janeway Using split function Host is lando Using split function Host is sisko Using split function Host is sulu
Re: I have done this before
by monk2b (Pilgrim) on Jul 13, 2007 at 19:24 UTC
    Thanks to toolic and saintly my problem is solved.


    I got 99 problems, but this ain't one.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://626499]
Approved by randyk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2022-05-25 05:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (84 votes). Check out past polls.

    Notices?