
- Operating System Tutorial
- OS - Home
- OS - Overview
- OS - Components
- OS - Types
- OS - Services
- OS - Properties
- OS - Processes
- OS - Process Scheduling
- OS - Scheduling algorithms
- OS - Multi-threading
- OS - Memory Management
- OS - Virtual Memory
- OS - I/O Hardware
- OS - I/O Software
- OS - File System
- OS - Security
- OS - Linux
- OS - Exams Questions with Answers
- OS - Exams Questions with Answers
- Operating System Useful Resources
- OS - Quick Guide
- OS - Useful Resources
- OS - Discussion
How to Count Word Occurrences in a Text File using Shell Script?
Linux shell scripting has many powerful tools to process the data in files. One such feature is to find patterns and count the number of occurrences of matched patterns. One such example is to count the number of occurrences of a specific word in a given file. This is achieved by combination of commands for pattern search and counting. Below are the approaches which can be used for this need.
Input file
Lets use the below file for demonstrating the examples.
$ cat inspire.txt Mastering anything needs practice. It also needs patience. And it needs time and other resources.
Using grep and wc
The grep command finds the pattern but it has some switches which can be used to print the pattern in new line every time it finds it. That is –o option. Also we can use the -i switch for ignoring the case of the word found. Finally, the result can be pipelined to wc to get the final count.In this example we count the number of occurrences of the word “needs”.
Approach
We grep for the word “needs” in the file. Use the –o switch to get each of the output as a new line. Use –i to ignore the case. Then pipeline it to wc in order to get the number of lines which is ultimately the number of occurrences of the word.
$ grep -o -i needs inspire.txt | wc -l
Running the above code gives us the following result −
3
Using tr command
The tr command translates one string to another. So we translate all the spaces into a new line and then grep for the pattern. Finally count the number of occurrences of the specific word by using the -c switch of grep for count and -i switch for ignoring the uppercase/lowercase word.
tr '[:space:]' '[
*]' < inspire.txt | grep -i -c it
Running the above code gives us the following result −
2
- Related Articles
- Count occurrences of a char in a text file
- Java program to count occurrences of a word in string
- C# program to count occurrences of a word in string
- How to find and replace the word in a text file using PowerShell?
- How to Find the Most Repeated Word in a Text File using Python?
- Python program to count occurrences of a word in a string
- How to Find Line Number of a Given Word in text file using Python?
- Write a python program to count occurrences of a word in string?
- How to count the number of words in a text file using Java?
- How to count the number of lines in a text file using Java?
- How to count the number of characters (including spaces) in a text file using Java?
- How to terminate a MongoDB shell script earlier?
- How to make a word count in textarea using JavaScript?
- How to replace all occurrences of a word in a string with another word in java?
- How to execute a Python file in Python shell?
