Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Pattern problem

by Anonymous Monk
on Jan 27, 2006 at 13:57 UTC ( #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 Host System Mars IP Status Host Jupiter down Host System #neptune IP Disk load Mars imminent Host System Venus IP ......
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
But I do want
Host System Pluto Host System Mars IP Host System Venus IP
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 Host System Mars IP Status Host Jupiter down Host System #neptune IP Disk load Mars imminent Host System Venus IP

    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


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

    $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; }
      next if ($text =~ /Host System (?!#)/);
      next if $text =~ /Host System (?!#)/;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-05-22 07:58 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (80 votes). Check out past polls.