Reznor
Reznor

Reputation: 1285

Returning single value for all grep matches through directory

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

Answers (3)

frankc
frankc

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

khachik
khachik

Reputation: 28693

You can use wc: grep "regexp" * | wc -l

Upvotes: 1

Chris Stratton
Chris Stratton

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

Related Questions