How to count occurrences of a word in all the files of a directory? But with count incremented only once per word per file
Asked Answered
M

1

2

This question is an additional constraint on this this question. I wanted achieve the word count by avoiding multiple counts for that same word in the same file? Eg : if word "aaa" appears in "file1.txt" 10 times, but count should increase only by 1 but not 10 & so on for other files too within a directory.

Marela answered 8/11, 2013 at 21:58 Comment(0)
V
5

So what you want is the number of files that contain that word. Easy:

grep -l word *|wc -l
Vinegar answered 8/11, 2013 at 22:6 Comment(3)
Exactly! It's working :) and to make it case sensitive and for exact word match(i.e., avoid nested matches), it would be : grep -l "\bword\b" *|wc -l, right? Correct me if I'm wrong.Marela
It is already case-sensitive. Do you mean case-insensitive?Vinegar
But if a directory has both directories and files within itself, this command applies only to the files in it but not its child directories recursively, right? Is there a way to count words in this scenario too?Marela

© 2022 - 2024 — McMap. All rights reserved.