Chinaunix首页 | 论坛 | 博客
  • 博客访问: 362284
  • 博文数量: 35
  • 博客积分: 2176
  • 博客等级: 大尉
  • 技术积分: 797
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-13 14:03
文章分类

全部博文(35)

文章存档

2012年(9)

2009年(14)

2008年(12)

我的朋友

分类:

2009-09-24 10:37:01

数据格式化

#!/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 );
}


阅读(969) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~