bash - Count multiple occurrences of some text for each file under a directory -
i trying count multiple occurrences of text each file under directory. following script close want not count multiple occurrences on same line:
grep -rc 'blah' /some/path --include \*.txt
for example given 2 files:
foo.txt blah, hey blah more text bar.txt blah
the above script produces:
foo.txt:1 bar.txt:1
but output looking is*:
foo.txt:2 bar.txt:1
i know total number of occurrences can found in 1 file using grep , piping results word count:
grep -oh 'blah' foo.txt|wc -l
how multiple files achieve output in example* above?
update
the best solution come follows:
find /some/path -name '*.txt'|awk '{print "echo -n '\''" $0 "\: '\'' && grep -oh '\''blah'\'' " $0 "|wc -l"}'|bash
grep -o prints each match on new line - count em up
dir=$1 grep -hor --include '*.txt' 'blah' $dir| uniq -c| # output after uniq # 3 dir/f0.txt:blah # 2 dir/f1.txt:blah awk '{file=gensub(/^.+\/|:.+/, "", "g", $2); print file ":" $1}'
Comments
Post a Comment