sandeep has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to open one by one all the fixed length files in a particular temp directory, read these files to find if they contain a particular Regular expression and add up number of times particular RE is found. I do not know how many files will be there in that directory,nor the full name of the file except that they will contain todays date in yyyymmdd format . Please help me as I am new to Perl programming.

Replies are listed 'Best First'.
Re: opening multiple files
by Zaxo (Archbishop) on Dec 19, 2004 at 04:56 UTC

    You can use glob to find all the file names matching that format. I'll use POSIX::strftime to generarte the glob pattern with today's date. Given the file names, I'll slurp each and count the number of matches to some $re.

    use POSIX 'strftime'; my $date = strftime '%Y%m%d', localtime; my $re = qr/whatever/; my $count = 0; for (glob "/path/to/*$date*") { local $/; # to slurp open my $fh, '<', $_ or warn $! and next; my $content = <$fh>; close $fh; $count += () = $content =~ /$re/g; } print $count,$/;

    The odd construction where $count is incremented is a common perl idiom for getting a list of matches and counting them. It places the bind-and-match in list context, then the resulting list is evaluated in numeric scalar context. Be cautious about capturing in $re. Multiple captures will produce extra elements in the list.

    After Compline,
    Zaxo

Re: opening multiple files
by atcroft (Abbot) on Dec 19, 2004 at 05:06 UTC

    You may want to look at the glob() function to get the list of files, such as:

    # Assumes filenames of the form *yyyymmdd.txt my $filepattern = sprintf("*%d%d%d.txt", 1900+(localtime)[5], 1+(localtime)[4], (localtime)[3]); foreach my $filename (glob($filepattern)) { # Process individual files herein }

    As to finding the count of the number of times the expression is found, perhaps something along the lines of:

    # Looking, for instance, for "/usr/.*/?bin/perl" my $regex = qr{(/usr/.*/?bin/perl)}gs; my ($line); { open(DF, $filename) or die("Can't open $filename for input: $!\n"); my @lines = <DF>; close(DF); $line = join('', @lines); } my @matches = $line =~ m/$regex/; printf "Filename: %40s Occurrances: %9d\n", $filename, scalar @matches;

    Hope that helps.

    Update: 19 Dec 2004 - Added values for sprintf function in first code section