I'm trying to parse 40+ text files that are in a directory for the word "Phone:" and print the phone number that comes after the string. I'm a super perl novice so any help is greatly appreciated. I had to comment out the strict or it wouldn't run,
Here's my code:
#!/usr/bin/perl
#use strict;
use warnings;
my $DIR = "/Ask";
opendir $DIR, '.' or die "opendir .: $!\n";
my @files = grep /\.txt$/i, readdir $DIR;
closedir $DIR;
print "Got ", scalar @files, " files\n";
my %seen = ();
foreach my $file (@files) {
open my $FILE, '<', $file or die "$file: $!\n";
while (<$FILE>) {
#print "test\n";
if (/^phone\s*(.*)\r?$/i) {
$seen{$1} = 1;
foreach my $addr ( sort keys %seen ) {
print "$addr\n";
}
}
}
close $FILE;
}
it sees the files but never seems to match the argument and print the results. I can also convert the files to html easily and parse them that way.
Thanks for all of the assistance so far. Here are a few more questions that have come up and an example of the files that I'm parsing:
Here's an example of the short files I'm parsing- Agilent Technologies,Inc. Headquarters. Toll-Free: +1 877-424-4536, phone: 4083458886.Fax: +1 408-345-8474 Address: 5301 Stevens Creek Blvd - I think the problem I'm having is that the phone: isn't always at the start of the line. If I modify my files and put it there all works well but I think the script has problems finding it in the middle of a row. Ideas?
:after/^phonein your regex?^phone\s*:\s*(.*)\r?$use strict;strictis equally good idea as putting tape over car indicator lights. In both cases it looks like it solves the problem.