0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I was looking for ! Thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.