6

I think the answer to this problem might be some sort of variant of the uniq function that allows you to count the number of times each line appears in a file:

sort file.txt | uniq -c

The problem for me is that I have used this uniq function to generate a line count, and because I am merging the output with other files, I end up with duplicate lines in the file that need further rationalisation.

For example, with the original uniq line count at the beginning of each line:

34 banana

23 apple

48 grapefruit

23 banana

12 apple

So what I need to get to is:

57 banana

35 apple

48 grapefruit

Is there some function that will SUM on the first field, in all cases where the remaining field(s) are identical?

0

2 Answers 2

11

An awk solution:

$ awk '{i[$2]+=$1} END{for(x in i){print i[x]" "x}}' file.txt
35 apple
48 grapefruit
57 banana

First awk makes an array whose index is the name (banana, apple, grapefruit) and sums up the values in the first column. At the end that array is printed.

1

I'd use perl.

#!/usr/bin/perl

use strict; 
use warnings;

my %count_of;

while ( <> ) {
   my ( $word) = m/(\w+)/;
   $count_of{$word}++;
}

foreach my $word ( sort { $count_of{$a} <=> $count_of{$b} } keys %count_of ) {
    print "$count_of{$word} $word\n";
}

Run it with perl script.pl file1 file2 file3 file4.

Alternatively - you probably want to just use cat.

cat file1 file2 file3 | sort | uniq -c
1

Not the answer you're looking for? Browse other questions tagged .