- [Narrator] Welcome to the next video of section four:
Finding the Frequency of Words Used in a Given File.
In the previous video, we have learned
how to use awk for advanced text processing.
In this video, we'll learn how to find the 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.
We can use associative arrays,
awk, sed, grep, and so on.
To solve this problem in different ways,
words are alphabetic characters delimited
by space or a period.
First, we should pass all the words in a given file
and then count of each word needs to be found.
Words can be passed by using regex with any of the tools
such as sed or grep.
We just saw the logic and ideas about the solution.
Now, let's create a shell script as follows:
A sample output is as follows:
Let's see how it works.
The highlighted file name is used to output only words.
The minus O option will print
the matching character sequence
delimited by a new line character.
Hence we receive words in each line.
Slash, B, is the word boundary character.
Brackets, alpha, is used as a
character class by alphabets.
The awk command is used to avoid
the iteration through each word.
Since awk, by default, executes statements
in the curly bracket block for each row.
We don't need to specify loop for doing that,
hence the count is incremented like this
by using the associate array.
Finally, in the end block,
we print the words and their count
by iterating through the words.
Awesome.
We successfully learned how to find the frequency of words
used in a given file.
In the next video,
we'll learn how to compress or decompress JavaScript.