PERL script that prints out the file names and file sizes and determines the average

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ormazd
    New Member
    • Mar 2010
    • 2

    PERL script that prints out the file names and file sizes and determines the average

    Hello,

    I was wondering if anyone might be able to help me with a little PERL script? I'm very new and I have been given a task to write a simple Perl script that prints out the file names and file sizes and determines the average file size from the directory listing found in the "files.txt" file. In other words, I need to add up the size of all of the files listed in the text document and divide by the number of files listed.

    Sample of "file.txt":

    Code:
    12/02/2002  12:16 AM             86016 a2p.exe
    10/10/2004  11:33 AM               393 avgfsznew.pl
    11/01/2003  04:42 PM             38124 c2ph.bat
    06/03/2002  03:01 AM              5484 config.pl
    11/07/2002  10:09 PM              1338 configPPM.pl
    07/13/2001  12:43 PM               647 crc32
    12/02/2002  12:26 AM              1065 crc32.bat
    12/02/2002  12:21 AM             24448 dprofpp.bat
    The output should look something like:

    Code:
     100             File name 1
    1000             File name 2
     100             File name 3
    Total files: 3 Average file size: 400 bytes
    So far I've written:

    Code:
    #!/usr/bin/perl
    open(MYFILE, "files")||
        die("Unable to open files.txt\n");
    
    my $count = 0;
    my $filetotal = 0;
    my $average = 0;
    
    while(<MYFILE>) {
        chomp $_;
        ($date, $time, $filesize, $filename) = split("\t", $_);
        print "$filesize\t\t$filename\n";
        $count++;
        $filetotal+=$filesize;
    }
       
    $average = $filetotal/$count;
    print("\nTotal Files: $count        Average file size: $average bytes\n");
    
    close(MYFILE);
    My output shows a bunch of blank lines and at the end I get the Total Files with the correct count, but the average file size is still 0. Can someone help me to find where I'm going wrong? I'm pretty sure it's got a lot to do with where I'm trying to split the columns. Any help would be greatly appreciated!

    Thanks!
  • Ormazd
    New Member
    • Mar 2010
    • 2

    #2
    After playing around, I finally got it to work!

    Code:
    #!/usr/bin/perl
    open(MYFILE, "files")||
        die("Unable to open files.txt\n");
    
    my $count = 0;
    my $filetotal = 0;
    my $average = 0;
    
    while(<MYFILE>) {
        chomp $_;
        ($date, $time, [B]$ampm, [/B]$filesize, $filename) = split([B]" "[/B], $_);
        print "$filesize\t\t$filename\n";
        $count++;
        $filetotal+=$filesize;
    }
       
    $average = $filetotal/$count;
    print("\nTotal Files: $count        Average file size: $average\n");
    
    close(MYFILE);
    I added an $ampm variable and split using space-delimited instead of tab-delimited. This resolved my issue!

    Now I just need to clean the output up a bit and I'll be in good shape.

    Comment

    Working...