Good companions, I wanted to share something that surely we have all had to face, to count the number of apparitions of the words in a file.
Imagine you have a file with the following content:

$ cat output.txt 
Selected Star1 is 2 frequency is 205
Selected Star2 is 7 frequency is 139
Selected Winner1 is 31 frequency is 4
Selected Winner2 is 12 frequency is 44
Selected Winner3 is 13 frequency is 26
Selected Winner4 is 36 frequency is 44
Selected Winner5 is 40 frequency is 40
Done!
Selected Star1 is 11 frequency is 4
Selected Star2 is 1 frequency is 7
Selected Winner1 is 34 frequency is 1
Selected Winner2 is 25 frequency is 32
Selected Winner3 is 16 frequency is 28
Selected Winner4 is 28 frequency is 43
Selected Winner5 is 21 frequency is 5
Done!
...

Using a unix script like the one below:

$ (tr ' ' '\n' | sort | uniq -c | awk '{print "number " $2" appeared "$1 " times."}') < output.txt 
number 1 appeared 43 times.
number 10 appeared 46 times.
number 101 appeared 8 times.
...
number 96 appeared 4 times.
number 98 appeared 3 times.
...

Have fun!

Alonso.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s