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

Hi,

I 've been trying to match for certain words in a file without success.

The flat file contains the following text:

printer llp is idle. enabled since Wed Oct 23 15:54:08 GMT 2002. avail +able. Form mounted: Content types: any Printer types: unknown Description: OPENprint printer llp Connection: direct Interface: /usr/lib/lp/model/standard On fault: write to root once After fault: continue Users allowed: (all) Forms allowed: (none) Banner not required Character sets: (none) Default pitch: Default page size: Default port settings: -opost printer ps is idle. enabled since Wed Oct 23 15:54:17 GMT 2002. availa +ble. Form mounted: Content types: postscript, simple Printer types: unknown Description: local printer Connection: direct Interface: /usr/lib/lp/model/net_lj4x On fault: write to root once After fault: continue Users allowed: (all) Forms allowed: (none) Banner not required Character sets: (none) Default pitch: Default page size: Default port settings:
I am trying to match on the first word that comes after the word "printer" which is "llp" or "ps". The fourth word after the word "printer" which is idle". The fifth word after "printer" which is "enabled".

Basically I am checking if the flat file contain these strings then do something to them.

I 've tried using Backreferences but without success. Could use some help.

--kirk

Added <code> and <p> tags - dvergin 2002-08-29

Replies are listed 'Best First'.
Re: Matching for the second,fourth, and fifth word in a text
by BrowserUk (Patriarch) on Dec 10, 2002 at 19:23 UTC

    I probably wouldn't use a regex

    #! perl -slw use strict; while (<DATA>) { next if not /^printer/; my @words = (split)[1,3,4]; print "@words"; } __DATA__

    prints

    C:\test>218863 llp idle. enabled ps idle. enabled

    Whether this works for you depends on whether you can live with (the presence of|having to chop) the '.' from the second word.


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Matching for the second,fourth, and fifth word in a text
by particle (Vicar) on Dec 10, 2002 at 19:03 UTC

    this ought do

    while( defined my $record = <FILE> ) { if( $record =~ m/(?x) \A ## anchor the match to the beginning of the reco +rd printer \s+ ## match the word 'printer', and one or more spa +ces (?: \w+ \s+ ){2} ## match two words followed by spaces idle \. \s+ ## match the word 'idle', followed by a period a +nd spaces enabled \s+ ## match the word 'enabled', followed by spaces / ) { ## $record matched, do your thing... } }

    ~Particle *accelerates*

Re: Matching for the second,fourth, and fifth word in a text
by dingus (Friar) on Dec 10, 2002 at 20:17 UTC
    Assuming your printer records contain a blank line and you can read the whole file in at once then you could loop aroung a //g; as in:
    local undef $/; open(FILE,"<printfile"); my $_ = <FILE>; # slurp close (FILE); print "printer '$1' $2 - $3\n" while (/^printer\s+(\w+)\s+\w+\s+(\w+)\ +.\s+(\w+)/msg); __OUTPUT__ 'llp' idle - enabled 'ps' idle - enabled

    Dingus


    Enter any 47-digit prime number to continue.
      local undef $/;
      Thatīll first clear $/, then localize it. Legal syntax, but not probably what you meant.
      $ perl -wle'$_ = "x"; { local undef $_ } print' Use of uninitialized value in print at -e line 1. $ perl -wle'$_ = "x"; { undef local $_ } print' x

      Makeshifts last the longest.

Re: Matching for the second,fourth, and fifth word in a text
by Aristotle (Chancellor) on Dec 11, 2002 at 14:04 UTC
    #!/usr/bin/perl -wl use strict; while(<>) { /^printer/ ? chomp : next; print join ", ", (/(\w+)/g)[1,3,4]; } __END__ llp, idle, enabled ps, idle, enabled

    Makeshifts last the longest.