How to Count Word Occurrences in a Text File using Shell Script?

Linux shell scripting provides powerful tools to process text files and analyze their content. One common task is counting word occurrences in a file using pattern matching and counting commands. This article demonstrates several approaches to count specific words in text files.

Sample Input File

Let's create a sample file to demonstrate our 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 patterns in text, and when combined with wc, we can count occurrences. The -o option prints each match on a separate line, while -i ignores case sensitivity.

Syntax

grep -o -i "word" filename | wc -l

Example

Count occurrences of the word "needs" in our file ?

grep -o -i "needs" inspire.txt | wc -l
3

This command works by:

  • grep -o − outputs each match on a separate line
  • -i − ignores case differences
  • wc -l − counts the number of lines (matches)

Using tr Command

The tr command translates characters. We can replace spaces with newlines, then use grep to count specific words directly.

Example

Count occurrences of the word "it" by converting spaces to newlines ?

tr '[:space:]' '[<br>*]' < inspire.txt | grep -i -c "it"
2

This approach:

  • tr '[:space:]' '[
    *]'
    − converts all whitespace to newlines
  • grep -c − counts matches directly
  • -i − ignores case

Using awk Command

AWK provides more flexibility for word counting with better word boundary detection ?

awk '{for(i=1;i<=NF;i++) if(tolower($i)=="needs") count++} END{print count+0}' inspire.txt
3

Comparison

Method Best For Case Handling Word Boundaries
grep + wc Simple pattern matching -i option Partial matches
tr + grep Word−by−word analysis -i option Better boundaries
awk Complex text processing tolower() function Exact word matching

Conclusion

Use grep -o | wc -l for simple word counting, tr + grep -c for better word boundary detection, or awk for precise word matching. Each method offers different levels of accuracy depending on your requirements.

---
Updated on: 2026-03-15T17:21:26+05:30

11K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements