计算目录下每个文件的多个文本的出现次数

时间:2016-08-23 23:34:56

标签: bash grep wc

我正在尝试计算目录下每个文件的多个文本。以下脚本接近我想要的但它不会在同一行上计算多次出现:

grep -rc 'blah' /some/path --include \*.txt

例如给出两个文件:

foo.txt
blah, hey blah
some more text

bar.txt
something blah

以上脚本产生:

foo.txt:1
bar.txt:1

但我要找的输出是*:

foo.txt:2
bar.txt:1

我知道可以使用grep在一个文件中找到总出现次数,然后将结果传递给字数:

grep -oh 'blah' foo.txt|wc -l

如何为多个文件执行此操作以实现输出,如上面的示例*中所示?

更新

我能想出的最佳解决方案如下:

find /some/path -name '*.txt'|awk '{print "echo -n '\''" 
    $0 "\: '\'' && grep -oh '\''blah'\'' " $0 "|wc -l"}'|bash

1 个答案:

答案 0 :(得分:1)

grep -o在新行上打印每个匹配项 - 然后计算它们

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}'
相关问题