[Chicago-talk] regular expression help

Richard Reina richard at rushlogistics.com
Wed Dec 30 09:58:44 PST 2009

I am trying to write a program that goes through emails in a thunderbird
Inbox finds the email address of any emails that have bounced back (
contained in failure notifications ) and creates a summary report of bad
email addresses.  The program is merely an adaptation of other perl code I
have that searches through files looking for regular expressions. 
However, I am getting confused on how to skip through this file.  What I am
intending in the code below is that once the string "could not be delivered"
is found that the program then begins to search for the next email address
listed, hence the search string "\@" and to let me know what that email is.
However, I'm getting confused with the iteration of the while loop and I am
not getting anywhere.  Can anyone advise on where I've gone wrong?  Perhaps
there's a better way of doing this?

Any help would be greatly appreciated.




 # short script to check a  for email errors
 use strict;
 use warnings;
 use File::Spec;
 use Carp;

 # set the directory that you want to search

 my $dir

 my @files_in_dir;
 my $files_in_dir;

 opendir(DIR, $dir) or die "Can't open directory: $!\n";
# line 19
 while (my $file = readdir(DIR)) {
     # use a regular expression to ignore file beginning with a period
     next if ($file =~ m/^\./);
     push(@files_in_dir, $file);

 my $i = 0;
 my $string = 'your message could not be delivered'; 

 foreach $files_in_dir (@files_in_dir) {
  if ($files_in_dir eq 'Inbox.msf') { #this is the one we need to search

 	 my $file_name = File::Spec->join($dir, $files_in_dir);
         open(LOG, '<', $file_name) or croak "Unable to open $file_name:

         my $j; # line counter
         while(<LOG>) {
        # line 51 test for string.  Put the string between the b and the
         if (/\b$string\b/i) {
 	     print "Apparent undelivered email " . $string . " FOUND in line " .
$j . " of " . $files_in_dir . "\n";
 	     print "  " . $_;
	     # look for the email address that comes after
	     my $bad_email = '\@';
	     	if (/\b$bad_email\b/i) {
			# this line contains the next email address
			print "This line contains the next email address " . $_;
			chomp (my $xx = <STDIN>);
		} else { # next line does not contain an email address
		 print "The next line did NOT contain an email address\n" . $_ . "\n";	
                 chomp (my $yy = <STDIN>);
    } # end of while

	} # end of if 


More information about the Chicago-talk mailing list