开发者

Returning single value for all grep matches through directory

开发者 https://www.devze.com 2023-02-01 10:29 出处:网络
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

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

精彩评论

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