Issue
I'm trying to create a for loop that counts the number of keywords in each file within a directory separately.
For Example if a directory has 2 files I would like the for loop to say
foo.txt = found 4 keywords
bar.txt = found 3 keywords
So far I have written the below but its give the total number keywords from all files instead of for each separate file.
The result I get is
7 keywords founds
Instead of the desired output above
Here is what I came up with
for i in *; do egrep 'The version is out of date|Fixed in|Identified the following' /path/to/directory* | wc -l ; done
Solution
Just use grep's -c
count option:
grep -c <pattern> /path/to/directory/*
You'll get something like:
bar.txt:2
foo.txt:1
Note this will count lines matched, not individual patterns matched. So if a pattern appears twice on a line, it will only count once.
Answered By - Alex Howansky