venky4289 has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unable to search for failed pattern in the files created in a directory.
by Happy-the-monk (Canon) on Jun 21, 2013 at 10:03 UTC | |
|
Re: Unable to search for failed pattern in the files created in a directory.
by Anonymous Monk on Jun 22, 2013 at 01:27 UTC |