简体   繁体   中英

grep a pattern in some files and print the sum in each file

I want to grep a pattern in some files and count the occurrence with the filename. Right know, if I use

grep -r "month" report* | wc -l

it will sum all instances in all files. So the output is a single value 324343 . I want something like this

report1: 3433
report2: 24399
....

The grep command will show the filename but will print every instance.

grep -c将为您提供每个文件的匹配计数:

grep -rc "month" report*

You need to pass each file to grep: echo report* | xargs grep -c month echo report* | xargs grep -c month .

If recursively, use find report* -exec grep month -Hc '{}' \\; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM