Perl - match e-mail addresses in multiple files

Hi, I'm trying to write a script that will check multiple files in a directory (all the relevant filenames begin "TT04.NOTES") for e-mail addresses, and then print these addresses to screen with a count at the bottom. I'm a bit of a novice with Perl but thought it would be the best tool for the job. This is what I've got so far:

#!/usr/bin/perl
use strict;
my @files;
my $filename;
my $line;
my $count;
my $dir = '/scripts';
opendir (DIR, $dir) or die $!;
while (my $file = readdir(DIR)) {
        next unless (-f "$dir/$file");
        next unless ($file =~ m/^TT04.NOTES/);
        push(@files, $file);
}
closedir(DIR);
print "@files";
foreach $filename (@files) {
        open (FILE, "$filename");
        While ( $line = <FILE> ) {
                if ($line =~ /[\w\.\-]+@[\w\.\-]+\w+/) {
                        print "$line";
                        $count++;
        }
        print "$count";
}
exit 0;

I'm getting a syntax error at line 19, near "if", and again at line 24, near "}". The regular expression looks ok to me, although as I say I'm not an expert with Perl. Can anybody point me in the direction of why I get a syntax error?

While ( $line = <FILE> ) {

should be

while ( $line = <FILE> ) {

You are also missing closing curly bracket for "foreach" loop.

1 Like

Thanks for the quick reply bartus11, that's working fine now.