Actually, I need to find the file when it gets created in the directory and i need to go inside the file and search for the failed pattern and i need to print the lines where that pattern is matched. Note: Here the file created is a log file so it will take some time to finish its creation. So now i need to search the file after its totally populated with the data.

#!/usr/bin/perl use strict; use warnings; use Carp; use File::Monitor; $| = 1; my $monitor = File::Monitor->new; push @ARGV, '.' unless @ARGV; while ( my $obj = shift ) { $monitor->watch( { name => $obj, recurse => 1 } ); } sub sendEmail { my ($to, $from, $subject, $message) = @_; my $sendmail = '/usr/lib/sendmail'; open(MAIL, "|$sendmail -oi -t"); print MAIL "From: $from\n"; print MAIL "To: $to\n"; print MAIL "Subject: $subject\n\n"; print MAIL "$message\n"; close(MAIL); } while ( 1 ) { my $val; sleep 1; my $time = localtime(); for my $change ( $monitor->scan ) { print $change->name, " changed\n"; my @val = $change->files_created; $val = join( ' ', @val ); if ( $val ) { printf( " %s\n", $val ); sendEmail("venkatesh.reddy\@us.fijistu.com","venkatesh.redd +y\@us.fijistu.com","file creation","$val created at $time.." ); + } xxxx : if($val =~ /(run.*\.log)$/){ print "log name: $1\n"; open(my $fh, "$1"); while(<$fh>) { if($_=~/failed/) { print "$_\n"; } } close($fh); } } }

But With the above code i am able to monitor the files created, but not able to search for the pattern required in the file created, so i am coming out of the loop in the starting of the log file creation, So now i need to stay in the loop till the file gets created and i need to search for log .

Can any one advise me in this issue, Thanks in Advance.


In reply to Unable to search for failed pattern in the files created in a directory. by venky4289

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.