+2 votes
in Operating Systems by (71.8k points)
I want to count the occurrence of a word in a large text file. How can I do using Linux command line?

1 Answer

+1 vote
by (346k points)
selected by
 
Best answer

You can use "grep" command in different ways to count the occurrence of a word in your text file. Have a look at the following examples:

Suppose you want to search the word "Hello" in the file abc.txt, you can use the following commands:

$cat abc.txt | grep -c Hello

$cat abc.txt | grep Hello | wc -l

If you need to count Hello but not prefixHello or Hellosuffix or prefixHellosuffix, you can use the following commands:

$ cat abc.txt | grep -c '\Hello'

$ cat abc.txt | grep '\Hello' | wc -l


...