By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,680 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,680 IT Pros & Developers. It's quick & easy.

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

P: 2
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":

Expand|Select|Wrap|Line Numbers
  1. 12/02/2002  12:16 AM             86016 a2p.exe
  2. 10/10/2004  11:33 AM               393 avgfsznew.pl
  3. 11/01/2003  04:42 PM             38124 c2ph.bat
  4. 06/03/2002  03:01 AM              5484 config.pl
  5. 11/07/2002  10:09 PM              1338 configPPM.pl
  6. 07/13/2001  12:43 PM               647 crc32
  7. 12/02/2002  12:26 AM              1065 crc32.bat
  8. 12/02/2002  12:21 AM             24448 dprofpp.bat
  9.  
The output should look something like:

Expand|Select|Wrap|Line Numbers
  1.  100             File name 1
  2. 1000             File name 2
  3.  100             File name 3
  4. Total files: 3 Average file size: 400 bytes
  5.  
So far I've written:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open(MYFILE, "files")||
  3.     die("Unable to open files.txt\n");
  4.  
  5. my $count = 0;
  6. my $filetotal = 0;
  7. my $average = 0;
  8.  
  9. while(<MYFILE>) {
  10.     chomp $_;
  11.     ($date, $time, $filesize, $filename) = split("\t", $_);
  12.     print "$filesize\t\t$filename\n";
  13.     $count++;
  14.     $filetotal+=$filesize;
  15. }
  16.  
  17. $average = $filetotal/$count;
  18. print("\nTotal Files: $count        Average file size: $average bytes\n");
  19.  
  20. 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!
Mar 1 '10 #1
Share this Question
Share on Google+
1 Reply


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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. open(MYFILE, "files")||
  3.     die("Unable to open files.txt\n");
  4.  
  5. my $count = 0;
  6. my $filetotal = 0;
  7. my $average = 0;
  8.  
  9. while(<MYFILE>) {
  10.     chomp $_;
  11.     ($date, $time, $ampm, $filesize, $filename) = split(" ", $_);
  12.     print "$filesize\t\t$filename\n";
  13.     $count++;
  14.     $filetotal+=$filesize;
  15. }
  16.  
  17. $average = $filetotal/$count;
  18. print("\nTotal Files: $count        Average file size: $average\n");
  19.  
  20. 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.
Mar 2 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.