Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Find a pattern

by fisher (Priest)
on Jan 28, 2011 at 09:11 UTC ( [id://884751]=note: print w/replies, xml ) Need Help??


in reply to Find a pattern

well, first of all, the very first line in your code:
file = "D:/perl/02.log";
I think 'file' intended to be a variable, isn't it? If yes, it should be written as '$file'.

Second, in the second line you use a wrong call to 'open'. First parameter to open should be a so called 'file handler', then the operation and file name:

open (FH, "<", $file) or die "cant open file";
third, regexp. I think you mean actually
if (/2 failed/)
...according to your question. What you wrote is 'a one to nine digits, then space, then the word "failed"'.

Replies are listed 'Best First'.
Re^2: Find a pattern
by ikegami (Patriarch) on Jan 28, 2011 at 09:50 UTC

    What you wrote is 'a one to nine digits, then space, then the word "failed"'.

    No. /\d{1-9}/ matches a digit followed by a "{" followed by a "1" followed by a "-" followed by a "9" followed by a "}".

    To match any number (for some definition of number), /\d+/ would do.

      To make this answer a little more helpful:
      The correct expression to match "a series of 1 to 9 digits" is:
      /\d{1,9}/ or /[0-9]{1,9}/ - note the comma, not dash

      1 to 9 digits between 1 and 9:
      /[1-9]{1,9}/

      Any number of digits between 1 and 9: /[1-9]+/
      Oh, thank you, my fault; I saw comma inplace of dash
Re^2: Find a pattern
by GrandFather (Saint) on Jan 28, 2011 at 09:35 UTC

    Actually the OP's open:

    open (FILE,$file) || die ("cant open file");

    is correct, but old school and frowned on. As you suggest the three parameter version of open is better, but even better is to use it with a lexical file handle. Using a lexical file handle ensures the file is closed when the file handle variable goes out of scope. The improved version of open is:

    open my $fileIn, "<", $file or die "Can't open $file: $!";

    Note too the file name mentioned in the die and the use of $! to give the system provided failure message. Both of those can help a lot when figuring out why an open failed.

    True laziness is hard work
Re^2: Find a pattern
by amvarma (Initiate) on Jan 28, 2011 at 10:04 UTC

    All,
    Thanks a lot
    My requirement is such that if the log contains the string
    like "2 failed" or "3 failed"..."<any number except 0>" failed.
    I have some other processing to do. So will the below code work.

    my $file = "D:/perl/02.log"; open my $inFile, '<', $file or die "cant open $file: $!"; # Look for errors while (<$inFile>) { if (/\d{1-9} failed/) { print "There are build errors (see line $.)\n"; exit 0; } }

    Thks AV

      It's a slightly tricky match to get right depending on just how fussy you really want to be. Using a negative look ahead helps cover most bases though. Consider:

      use strict; use warnings; my $str = <<STR; Errors: 0 failed Errors: 01 failed Errors: 1 failed Errors: 10 failed Errors: None failed Ben10 failed STR open my $fIn, '<', \$str; while (<$fIn>) { print if /\b(?!0+\b)\d{1,9} failed/; }

      Prints:

      Errors: 01 failed Errors: 1 failed Errors: 10 failed

      The (?!0+\b) is the negative look ahead that fails the match if a zero value (regardless of the number of 0 digits) is found. The \b's are there to ensure all the digits in the number are matched.

      True laziness is hard work
      Won't work. Use /[1-9] failed/ for that. \d{1-9} would actually parse a digit followed by '{1-9}' or a number with 1 to 9 digits if the '-' were a ','. More info in perlre.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-25 20:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found