473,418 Members | 2,072 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

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

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
1 8202
Ormazd
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

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

Similar topics

9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
0
by: Kirt Loki Dankmyer | last post by:
So, I download the latest "stable" tar for perl (5.8.7) and try to compile it on the Solaris 8 (SPARC) box that I administrate. I try all sorts of different switches, but I can't get it to compile....
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
9
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
3
by: Jerim79 | last post by:
Here it is: <?php if($_SERVER=='POST'){ $Number=$_POST; $Email=$_POST; $Number2=0; $error=0;
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
by: gnidoc | last post by:
This is a small perl script I wrote to check mirror lists (files filled with one URL per line). It prints " http://..." when an URL can't be retrieved and just prints the url when everything goes...
2
by: Edwin.Madari | last post by:
#your thought is right. ======================================================= def sizes2fields(sizes): d = begin = 0 for i in sizes: if begin: end = begin + i else: end = i...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.