用来统计文件中每一行出现的此处的Perl脚本

 #源文件如下:-------

#!/usr/bin/perl
use warnings;
use strict;
use vars qw/ %opts /;

&usage() if("@ARGV" =~ /--help/);
sub init(){
        use Getopt::Std;
        my $opt_str = 'esh';
        getopts( "$opt_str",/%opts ) or &usage();
        &usage() if $opts{h};
}
sub usage(){
        print STDERR '
        this program does../n
        usage:$0 [-esh] file1 file2 file3 ... fileN

        -h      : this (help) messages
        -e      : desc count
        -s      : display /s+

        example: $0 -e -s myfile
';
exit(0);
}
&init();
my @Contents= ();
my %Line_Count =();
my ($whidth,$wc_total) = (0,0);
my @sorted = ();

my @myFiles = &getFileFormAgument();
if(@myFiles > 0 ){
        foreach my $f (@myFiles){
                open (MYFILE,$f) or die "$f: Can't Open!$!/n";
                @Contents = <MYFILE>;
                close(MYFILE);
                &wcEverLine();
                if(exists $opts{"e"}){
                        &escLineByCount();
                        foreach my $hashref (@sorted) {
                                my ($k, $v) = each %$hashref;
                                printf "  %${whidth}s %s/n",$v,$k;
                        }
                }else{
                        while(my ($k,$v)= each(%Line_Count)){
                                printf "  %${whidth}s %s/n",$v,$k;
                        }
                }
                printf "  %${whidth}s total/n/n",$wc_total;

                %Line_Count = ();
                ($whidth,$wc_total) = (0,0);
                @sorted = {};
        }
}else{
        @Contents =<>;
        &wcEverLine();
        if(exists $opts{"e"}){
                &escLineByCount();
                foreach my $hashref (@sorted) {
                        my ($k, $v) = each %$hashref;
                        printf "  %${whidth}s %s/n",$v,$k;
                }
        }else{
                while(my ($k,$v)= each(%Line_Count)){
                        printf "  %${whidth}s %s/n",$v,$k;
                }
                printf "  %${whidth}s total/n/n",$wc_total;
        }
}
sub wcEverLine{
        foreach my $l (@Contents){
                chomp $l;
                if(exists $opts{"s"}){$l =~ s/^/s+//;$l =~ s//s+$//;}
                $wc_total++;
                $Line_Count{$l}++;
                $whidth = length($Line_Count{$l}) if $whidth < length($Line_Count{$l});
        }
        $whidth =length($wc_total) if $whidth < length($wc_total);
}
sub escLineByCount{
        @sorted = map { { ($_ => $Line_Count{$_}) } }
                sort { $Line_Count{$a} <=> $Line_Count{$b} or $a cmp  $b } keys %Line_Count;
}
sub getFileFormAgument {
        my @augs = ();
        foreach my $aug (@ARGV){
                if($aug =~ /^-/){next;}else{
                        push(@augs,$aug);
                }
        }
        return @augs;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值