用于计算列中每个匹配项的数量的Perl脚本

时间:2016-05-25 18:13:45

标签: perl file-handling

我想计算每行在文件中出现的次数

给出一列像

这样的信息
cat woman and bain
bat man
hat woman
cat man
bat man and friends
fat man
hat woman
+bat man
cat woman and bain
super bat man
bat man
bat man

我希望收到类似

的内容
2 cat woman and bain 
3 bat man
2 hat woman
1 cat man
1 bat man and friends
1 fat man
1 +bat man
1 super bat man

我有一些未完成的代码,如

open OUTFILE, '>text.txt';
while (<>){
        if( text.txt =~ $_ ){
                #increment the count for this occurence
        }else{
                print OUTFILE $_;
        }
}

2 个答案:

答案 0 :(得分:5)

$ sort file | uniq -c
      1 +bat man
      3 bat man
      1 bat man and friends
      1 cat man
      2 cat woman and bain
      1 fat man
      2 hat woman
      1 super bat man

$ perl -e'
   ++$h{$_} while <>;
   printf "%7d %s", $h{$_}, $_ for sort keys(%h);
' file
      1 +bat man
      3 bat man
      1 bat man and friends
      1 cat man
      2 cat woman and bain
      1 fat man
      2 hat woman
      1 super bat man

sort替换为sort { $h{$b} <=> $h{$a} || $a cmp $b },以将输出排序为降序计数。

答案 1 :(得分:0)

我愿意:

open INFILE, "<", "input.txt";
open OUTFILE, "+>", "output.txt";
my %hash;
foreach my $line (<INFILE>){
    chomp $line; # Remove trailing newline
    $hash{$line}++;
}

while (my($key,$val) = each %hash) {
    print OUTFILE "$val - $key"
}