Perl - match e-mail addresses in multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - match e-mail addresses in multiple files
# 1  
Old 04-18-2011
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:

Code:
#!/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?

Last edited by pludi; 04-18-2011 at 04:08 PM..
# 2  
Old 04-18-2011
Code:
While ( $line = <FILE> ) {

should be
Code:
while ( $line = <FILE> ) {

You are also missing closing curly bracket for "foreach" loop.
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 04-19-2011
Thanks for the quick reply bartus11, that's working fine now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl match multiple numbers from a variable similar to egrep

I want to match the number exactly from the variable which has multiple numbers seperated by pipe symbol similar to search in egrep.below is the code which i tried #!/usr/bin/perl my $searchnum = $ARGV; my $num = "148|1|0|256"; print $num; if ($searchnum =~ /$num/) { print "found"; }... (2 Replies)
Discussion started by: kar_333
2 Replies

2. Shell Programming and Scripting

Match records from multiple files

Hi, I have 3 tab delimited text files which look like this. File1: PROTEINID DESCRIPTION PEPTIDES FRAMES GB://115298678 _gi_115298678_ref_NP_000055.2_ complement C3 precursor 45 55 GB://4502027 _gi_4502027_ref_NP_000468.1_ serum albumin preproprotein 34 73 Entrez://strain 11128 /... (3 Replies)
Discussion started by: Vavad
3 Replies

3. Shell Programming and Scripting

last occurrence of a match through multiple files

Hi all, I have a lot of files with extension ".o" and I would like to extract the 10th line after (last) occurrence of a given string in each of the files. I tried: $ grep "string_to_look_for" *.o -A 10 | tail -1 but it gives the occurrence in the last file with extension .o ... (1 Reply)
Discussion started by: f_o_555
1 Replies

4. Shell Programming and Scripting

Echo - Sending mail to multiple addresses

Hi, If I want my script to send a mail to multiple recipients I can do the following: if then echo $err_string1 | mailx -s "UAT CPU ALERT" 1@email.com echo $err_string1 | mailx -s "UAT CPU ALERT" 2@email.com fi Can this also be done something like: ... (1 Reply)
Discussion started by: runnerpaul
1 Replies

5. Shell Programming and Scripting

Define multiple mail recipents in a variable in perl

hi I have a perl script from which I call a shell script and pass mail variable to it. The mail works fine if I give 1 recipient but fails for multiple. conv.pl:- $mialing = "anu\@abc.com" rest.sh $mialing rest.sh mail -s "hi" $1 This works fine But I need to define multiple... (2 Replies)
Discussion started by: infyanurag
2 Replies

6. Shell Programming and Scripting

Perl: Printing Multiple Lines after pattern match

Hello People, Need some assistance/guidance. OUTLINE: Two files (File1 and File2) File1 has some ids such as 009463_3922_1827 897654_8764_5432 File2 has things along the lines of: Query= 009463_3922_1827 length=252 (252 letters) More stufff here ... (5 Replies)
Discussion started by: Deep9000
5 Replies

7. Shell Programming and Scripting

How can i send mail to multiple addresses in same domain in bash?

Suppose i have a txt file that is the list of the addresses,something like: lala0045 john james lala0234 george james and i want to send an email to lala0045@blabla.com and lala0234@blabla.com,the same domain...what is the exact syntax i should use in my script? there is a command... (10 Replies)
Discussion started by: bashuser2
10 Replies

8. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies

9. Shell Programming and Scripting

read and match multiple lines in perl

Could any one tell me how to read and match multiple lines in perl? Did this code below still work in this situation? while (<FILE>) { if (/ /) { } } Thanks a lot! (5 Replies)
Discussion started by: zx1106
5 Replies

10. Forum Support Area for Unregistered Users & Account Problems

keeps telling me my email addresses don't match

HI there, Trying to register but it keeps telling me my email address doesn't match. I tried several times. I even tried closing out and coming back to the page. Thanks (0 Replies)
Discussion started by: r0k3t
0 Replies
Login or Register to Ask a Question