Link to home
Start Free TrialLog in
Avatar of bt707
bt707Flag for United States of America

asked on

while loop and getting current file name

I have a script that prints out this:

Date    Calendars      Events        Tasks  
        8730            1968141         63950
        8799          1955435         62543
        8800          1955782         62546
        8900          1970178         62531

what I can't get to work is I need to fill in the Date column. The name of each file in @files is the date the
log was created, such as

20070923
20070924
20070925
20070926
20070927
20070928
20070929
20070930
20071001
20071002
20071003
20071004
20071005
20071006
20071007
20071008
20071009


while it loops through the files and gets the info I need I need to get the file name that it is working on
and insert it in the Date column, I've tried several ways but can't figure out what I need to do here.


This is the script I'm useing:

#!/usr/bin/perl
use strict;

chdir "/log/cals";
my @files = `ls -1rt *`;

print join(qq{\t},qw{Date Calendars Events Tasks},"\n");  

foreach my $files (@files) {
next unless /^\s*$/;
open FH, $files or die "cannot open $files";
while (<FH>){
next if /^\s*$/;
    my ($cals) = (/Total number of calendars:\s+(\d+)$/);
    my ($events) = (/Total number of events:\s+(\d+)$/);
    my ($tasks) = (/Total number of tasks:\s+(\d+)$/);
    print "\t$cals " if $cals;
    print "\t$events " if $events;
    print "\t$tasks\n" if $tasks;
 }
}


The info in the files I'm searching through have lines like this in them:


Total number of calendars:    10898
Total number of events:       2388223
Total number of tasks:        80819

I can do a foreach and print out all the $files for @files but can't get it to print the file that I'm working on where I can
put it in the Date column.


Thanks for any help on this.
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bt707

ASKER

Thanks ozo, just what I needed, I had tried something close to that but sure wasn't getting it to work.

Thanks,