开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜