Count lines containing word

text processing

I have a file with multiple lines. I want to know, for each word that appears in the total file, how many lines contain that word, for example:

0 hello world the man is world
1 this is the world
2 a different man is the possible one

The result I'm expecting is:

0:1
1:1
2:1
a:1
different:1
hello:1
is:3
man:2
one:1
possible:1
the:3
this:1
world:2

Note that the count for "world" is 2, not 3, since the word appears on 2 lines. Because of this, translating blanks to newline chars wouldn't be the exact solution.

Best Answer

Another Perl variant, using List::Util

$ perl -MList::Util=uniq -alne '
  map { $h{$_}++ } uniq @F }{ for $k (sort keys %h) {print "$k: $h{$k}"}
' file
0: 1
1: 1
2: 1
a: 1
different: 1
hello: 1
is: 3
man: 2
one: 1
possible: 1
the: 3
this: 1
world: 2
Related Question