#!/usr/bin/perl
use strict;
use warnings;
no strict "vars";
use constant DEBUG => 0;
$GIGABYTE = 1024 * ( $MEGABYTE = 1024 * ( $KILOBYTE = 1024 ) );
die("Usage: $0 [input filename(s)]\n") if (scalar @ARGV == 0);
my $outname = $ARGV[0];
$outname =~ s/(.*)\.(.*)/$1/g;
$outname = join("", $outname, $$, ".txt");
open my $wfile, ">", "$outname" or die "Failed open file:$!\n";
my $sum = 0;
my $lines = 0;
foreach my $filename (@ARGV) {
my ($szsent, $szreceived) = 0;
my $url;
print $filename, "\n" if (DEBUG == 1);
open $fh, $filename or die "Failed open file:$!\n";
while (<$fh>) {
next if ($. == 1);
next if /^\s+$/;
$lines++;
my @fields = split(",",$_);
print "@fields\n" if (DEBUG == 1);
$szsent += $fields[3];
$szreceived += $fields[4];
$url = $fields[8];
print "$url\n" if (DEBUG == 1);
print $wfile $lines ," :$url\n";
}
$sum += $szsent;
$sum += $szreceived;
print "The file $filename size is : ", FormatNumber($sum) ,"\n" if (DEBUG == 1);
close $fh;
}
print "url all files size is: ", FormatNumber($sum) ,".\n";
close $wfile;
print "Then end!\n";
sub FormatNumber
{
my( $Number ) = @_;
my( $Suffix ) = "B";
my $K = 1024;
my $M = 1024 * $K;
if( $GIGABYTE <= $Number )
{
$Suffix = "G";
$Number /= $GIGABYTE;
}
elsif( $MEGABYTE <= $Number )
{
$Suffix = "M";
$Number /= $MEGABYTE;
}
elsif( $KILOBYTE <= $Number )
{
$Suffix = "K";
$Number /= $KILOBYTE;
}
$Number =~ s/(\.\d{0,1})\d*$/$1/;
$Number =~ s/^(-?\d+)(\d{3})/$1,$2/;
return( $Number . $Suffix );
}
|