Reputation: 31
I have a file and I want to know how many times does a word is inside that file.(NOTE: A row can have the same word)
Upvotes: 2
Views: 92
Reputation: 63974
Yes, i know you want a grep
solution, but my favorite perl with the rolex operator can't missing here... ;)
perl -0777 -nlE 'say $n=()=m/\bYourWord\b/g' filename
# ^^^^^^^^
if yoy want match the YourWord
surrounded with another letters like abcYourWordXYZ
, use
perl -0777 -nlE 'say $n=()=m/YourWord/g' filename
Upvotes: 0
Reputation: 195289
awk solution:
awk '{s+=gsub(/word/,"&")}END{print s}' file
test:
kent$ cat f
word word word
word
word word word
kent$ awk '{s+=gsub(/word/,"&")}END{print s}' f
7
you may want to add word boundary if you want to match an exact word.
Upvotes: 0
Reputation: 17288
Use the grep -c option to count the number of occurences of a search pattern.
grep -c searchString file
Upvotes: 0
Reputation: 1171
You can use this command. Hope this wil help you.
grep -o yourWord file | wc -l
Upvotes: 4