Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Pattern problem

by Anonymous Monk
on Jan 27, 2006 at 13:57 UTC ( [id://525969]=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to match a pattern that contains several words but only if the 3rd word doesn't begin with a #. e.g.
sample data.... Status Host Pluto up Host System Pluto 10.1.4.11 Host System Mars IP 10.1.3.10 Status Host Jupiter down Host System #neptune IP 10.10.10.10 Disk load Mars imminent Host System Venus IP 12.1.2.13.14 ......
The input data can stretch to thousands of records in random sort order. I need to pick out the records which begin "Host System" but not those where the 3rd word begins with # e.g. from the above sample data I don't want
Host System #neptune IP 10.10.10.10
But I do want
Host System Pluto 10.1.4.11 Host System Mars IP 10.1.3.10 Host System Venus IP 12.1.2.13.14
I tried doing this
if $text =~ /Host System (?!#)/ next;
This didn't work.
How do I get the pattern match to only pick up records where the 3rd field doesn't start with a # ?
bear in mind that the 3rd field can begin with pretty much any character/number

Replies are listed 'Best First'.
Re: Pattern problem
by aquarium (Curate) on Jan 27, 2006 at 14:17 UTC
    print "match" if($text =~ /Host System [^#]/);
    the hardest line to type correctly is: stty erase ^H

      Darn those global monks! Here it is, 6:20 am local time, and I figured, heck, nobody else is awake, I'll get a chance to be the first to answer a question. But I see it is 2:20 pm your time -- this monastery never sleeps! :)

Re: Pattern problem
by japhy (Canon) on Jan 27, 2006 at 14:35 UTC
    I'd like to know how you've determined /Host System (?!#)/ doesn't work. It should work fine.
    while (<FH>) { print if /^Host System (?!#)/; # or perhaps for more whitespace safety # print if /^Host\s+System\s+(?![#\s])/ }

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Hmm this is both embarrasing and interesting at the same time. I've just redone my tests and it does work using
      /Host System (?!#)/
      Most people seem to have suggested that it shouldn't do though based on the previous answers ??

        Nah, they just did the straightforward reply to your OQ "How do I ..." Congrats to japhy for thinking it through.

        Be Appropriate && Follow Your Curiosity
Re: Pattern problem
by ptum (Priest) on Jan 27, 2006 at 14:18 UTC

    Maybe something as simple as this?

    #!/usr/local/bin/perl use strict; while (<DATA>) { if (/Host System [^#]/) { print "$_"; } } __DATA__ Status Host Pluto up Host System Pluto 10.1.4.11 Host System Mars IP 10.1.3.10 Status Host Jupiter down Host System #neptune IP 10.10.10.10 Disk load Mars imminent Host System Venus IP 12.1.2.13.14

    No good deed goes unpunished. -- (attributed to) Oscar Wilde
      or simply
      perl -ne 'print if /Host System [^#]/;' records.txt


      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      grrr.... argh!
        perl -pe 's/(Host System [^#].+)|.+/$1/gs' records.txt
        =****

        Mago
        mago@rio.pm.org


Re: Pattern problem
by spadacciniweb (Curate) on Jan 27, 2006 at 14:21 UTC
    Why
    $text =~ /Host System (?!#)/
    ??

    Correct:
    $text =~ /Host System [^#]/

      $text =~ /Host System (?!#)/ is correct too. However, [^#] is probably faster and an acceptable replacement in this case.

      The problem is
      if $text =~ /Host System (?!#)/ next;
      was used instead of
      if ($text =~ /Host System (?!#)/) { next; }
      or
      next if ($text =~ /Host System (?!#)/);
      or
      next if $text =~ /Host System (?!#)/;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-24 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found