Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i find the count of multiple words in a text file?

Tags:

linux

shell

I am able to find the number of times a word occurs in a text file, like in Linux we can use:

cat filename|grep -c tom

My question is, how do I find the count of multiple words like "tom" and "joe" in a text file.

like image 206
Rakesh Avatar asked Aug 24 '11 07:08

Rakesh


People also ask

How do I count how many times a word appears in a file?

You can use grep command to count the number of times "mauris" appears in the file as shown. Using grep -c alone will count the number of lines that contain the matching word instead of the number of total matches.

How do you count the number of repeated words in Python?

Python Code:def word_count(str): counts = dict() words = str. split() for word in words: if word in counts: counts[word] += 1 else: counts[word] = 1 return counts print( word_count('the quick brown fox jumps over the lazy dog. '))


1 Answers

Since you have a couple names, regular expressions is the way to go on this one. At first I thought it was as simple as just a grep count on the regular expression of joe or tom, but fount that this did not account for the scenario where tom and joe are on the same line (or tom and tom for that matter).

test.txt:

tom is really really cool!  joe for the win!
tom is actually lame.


$ grep -c '\<\(tom\|joe\)\>' test.txt
2

As you can see from the test.txt file, 2 is the wrong answer, so we needed to account for names being on the same line.

I then used grep -o to show only the part of a matching line that matches the pattern where it gave the correct pattern matches of tom or joe in the file. I then piped the results into number of lines into wc for the line count.

$ grep -o '\(joe\|tom\)' test.txt|wc -l
       3

3...the correct answer! Hope this helps

like image 171
Travis Nelson Avatar answered Oct 20 '22 18:10

Travis Nelson