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 r开发者_如何学Goeturn 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?
You can use wc
: grep "regexp" * | wc -l
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}'
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.
精彩评论