0

I'm just trying to count the occurrences of a word without writing an iteration file by file. I don't mind which kind of file it is. The closest I got is:

COUNT=$(grep -r -n -i "theWordImSearchingFor" .)
echo $COUNT

I thought about splitting that by spaces, but the problem is the output does not contain just the filename and the line but also the content (and that may have tons of spaces). w.g. I got:

./doc1.txt:29: This is the content containing theWordImSearchingFor but also other stuff  
./doc1.txt:43: This is another line containing theWordImSearchingFor 
./dir123/doc2.txt:339: .This is another...file...theWordImSearchingFor....

Any idea on how to keep it simple? TIA

gal007
  • 6,911
  • 8
  • 47
  • 70

1 Answers1

1

To count the number of occurrences of a specific word, you need to use the same layout of code, but simpler. There are many ways to do this, but there are two much simpler versions of the word count that you have listed here. The much two simpler versions,

1st way

2nd way

They both should work, unless problem with package installation.