JackWM
JackWM

Reputation: 10565

Count the distinct words

I have an input file is like:

com.a.b
com.c.d
com.a.b
com.e.f
com.e.f
com.e.f

And I would like the output to be like:

com.a.b 2
com.c.d 1
com.e.f 3

Upvotes: 1

Views: 137

Answers (1)

Chris Seymour
Chris Seymour

Reputation: 85883

If you don't mind a slight variation in the given output then the traditional approach is with sort and uniq:

$ sort file | uniq -c
      2 com.a.b
      1 com.c.d
      3 com.e.f

Otherwise use a scripting language that has associative arrays such as awk:

$ awk '{a[$0]++}END{for(k in a)print k,a[k]}' file
com.e.f 3
com.c.d 1
com.a.b 2

Pipe to sort for ordering:

$ awk '{a[$0]++}END{for(k in a)print k,a[k]}' file | sort
com.a.b 2
com.c.d 1
com.e.f 3

Upvotes: 9

Related Questions