-5

我想计算多个文件中的唯一行:

f1.txt

2039-177 GRAPE
2039-177 GRAPE
2039-176 GRAPE
2039-176 GRAPE
234-450  APPLE

f2.txt

690-346 MANGO
690-346 MANGO
347-348 GRAPE
4350-350 MANGO

期望的输出

Total no:of grapes  - 3
Total no:of mangoes - 2
Total no:of apples  - 1
4

2 回答 2

2

纯 Perl:

$ perl -lane '$seen{$_}++ or ++$n{lc $F[1]};
  END{print "Total no:of ${_}s - $n{$_}"
        for sort { $n{$b} <=> $n{$a} } keys %n}' f1.txt f2.txt
Total no:of grapes - 3
Total no:of mangos - 2
Total no:of apples - 1

管道:

$ cat f1.txt f2.txt | uniq | \
  perl -lanE '++$n{lc$F[1]};END{say"Total no:of ${_}s - $n{$_}"for keys%n}' | \
  sort -nrk5
Total no:of grapes - 3
Total no:of mangos - 2
Total no:of apples - 1
于 2013-03-11T10:50:02.990 回答
0

大概是这样的?(未经测试。)

此解决方案需要输入文件的名称作为命令行上的参数。

use strict;
use warnings;

use List::Utils 'max';

my %count;
$count{ (split)[1] }++ while <>;

my $len = 1 + max map length, keys %count;

for (keys %count) {
    printf "Total no:of %-*s - %d\n", $len, lc $_.'s', $count{$_};
}
于 2013-03-11T09:06:20.260 回答