O'Reilly logo

Linux Shell Scripting Cookbook by Sarath Lakshman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Frequency of words used in a given file

Finding the frequency of words used in a file is an interesting exercise to apply the text processing skills. It can be done in many different ways. Let's see how to do it.

Getting ready

We can use associative arrays, awk, sed, grep, and so on to solve this problem in different ways.

How to do it...

Words are alphabetic characters delimited by space and dot. First we should parse all the words in the given file. Hence the count of each word needs to be found out. Words can be parsed by using regex with any of the tools such as sed, awk, or grep.

To find out the count of each word, we can have a different approach. One way of doing it is to loop through each word, and then use another loop to go through the words ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required