Reputation: 1285
I'm using Grep to go through every file in a directory and match a word. It will return the match count for each file, but I was wondering if there was a way to return the total of all the matches in one return?
Upvotes: 0
Views: 404
Reputation: 11473
I don't know if grep can do it, but it's easy to do with awk:
grep -c foo * | awk -F: '{sum += $2} END {print sum}'
Upvotes: 1
Reputation: 40357
how about
cat * | grep -c regexp
You'll probably get 'xxx is a directory' and similar warnings to stderr unless you use some flag to suppress them, but the count seems to work.
Upvotes: 0