shell Grep递归和计数

bgtovc5b  于 2023-01-26  发布在  Shell
关注(0)|答案(7)|浏览(147)

需要搜索一个有很多子目录的目录中的字符串文件:
我正在使用:

grep -c -r "string here" *

如何计算找到的总数?
我怎样才能输出到文件只有那些文件至少有一个示例?

brvekthn

brvekthn1#

使用Bash的进程替换,我相信这就是您想要的输出(如果不是,请澄清问题)。

grep -r "string here" * | tee >(wc -l)

这将正常运行grep -r,输出将同时发送到stdout和wc -l进程。

vkc1a9a2

vkc1a9a22#

它对我很有效(它获取在每个文件中找到的'string here'的总数)。但是,它不显示搜索的所有文件的总数。下面是你可以获取它的方法:

grep -c -r 'string' file > out && \
    awk -F : '{total += $2} END { print "Total:", total }' out

列表将输入输出,总数将发送到STDOUT。
下面是Python2.5.4目录树的输出:

grep -c -r 'import' Python-2.5.4/ > out && \
    awk -F : '{total += $2} END { print "Total:", total }' out
Total: 11500

$ head out
Python-2.5.4/Python/import.c:155
Python-2.5.4/Python/thread.o:0
Python-2.5.4/Python/pyarena.c:0
Python-2.5.4/Python/getargs.c:0
Python-2.5.4/Python/thread_solaris.h:0
Python-2.5.4/Python/dup2.c:0
Python-2.5.4/Python/getplatform.c:0
Python-2.5.4/Python/frozenmain.c:0
Python-2.5.4/Python/pyfpe.c:0
Python-2.5.4/Python/getmtime.c:0

如果您只想获取出现'string'的行,请更改为:

grep -c -r 'import' Python-2.5.4/ | \
    awk -F : '{total += $2; print $1, $2} END { print "Total:", total }'

它将输出:

[... snipped]
Python-2.5.4/Lib/dis.py 4
Python-2.5.4/Lib/mhlib.py 10
Python-2.5.4/Lib/decimal.py 8
Python-2.5.4/Lib/new.py 6
Python-2.5.4/Lib/stringold.py 3
Total: 11500

您可以更改文件($1)和每个文件的计数($2)的打印方式。

cgyqldqp

cgyqldqp3#

AWK的一些解决方案:

grep -r "string here" * | awk 'END { print NR } 1'

接下来是总计数、文件数和每个文件的匹配数,显示每个文件的第一个匹配(要显示所有匹配,请将条件更改为++f[$1]):

grep -r "string here" * | 
    awk -F: 'END { print "\nmatches: ", NR, "files: ", length(f); 
                   for (i in f) print i, f[i] } !f[$1]++'

第一个解决方案的输出(在目录中搜索“boost::“。我手动剪切了一些太长的行,使它们适合水平方向):

list_inserter.hpp:            return range( boost::begin(r), boost::end(r) );
list_of.hpp:            ::boost::is_array<T>,
list_of.hpp:            ::boost::decay<const T>,
list_of.hpp:            ::boost::decay<T> >::type type;
list_of.hpp:        return ::boost::iterator_range_detail::equal( l, r );
list_of.hpp:        return ::boost::iterator_range_detail::less_than( l, r );
list_of.hpp:        return ::boost::iterator_range_detail::less_than( l, r );
list_of.hpp:        return Os << ::boost::make_iterator_range( r.begin(), r.end() );
list_of.hpp:            return range( boost::begin(r), boost::end(r) );
list_of.hpp:            return range( boost::begin(r), boost::end(r) );
list_of.hpp:            return range( boost::begin(r), boost::end(r) );
ptr_list_of.hpp:                          BOOST_DEDUCED_TYPENAME boost::ptr_...
ptr_list_of.hpp:        typedef boost::ptr_vector<T>       impl_type;
13

第二个的输出

list_inserter.hpp:            return range( boost::begin(r), boost::end(r) );
list_of.hpp:            ::boost::is_array<T>,
ptr_list_of.hpp:                          BOOST_DEDUCED_TYPENAME boost::ptr_...

matches:  13 files:  3
ptr_list_of.hpp 2
list_of.hpp 10
list_inserter.hpp 1

结果中的颜色很漂亮(grep为--color=always),但是在通过awk传输时会中断。所以最好不要启用它们,除非你想让你所有的终端都着色:)干杯!

8iwquhpp

8iwquhpp4#

我将尝试find和grep的组合。

find . | xargs grep -c "string here"

不管怎么说,grep -c -r "string here" *对我很有效(Mac OS X)。

bogh5gae

bogh5gae5#

grep -rc "my string" ./ | grep :[1-9] >> file_name_by_count.txt

就像个护身符。

r6l8ljro

r6l8ljro6#

要仅输出匹配的文件名,请用途:

grep -r -l "your string here" .

它将输出一行,其中包含与搜索表达式匹配的每个文件的文件名。

csga3l58

csga3l587#

“如何计算找到的总数?”
grep -roh "string here" | grep -v "^Binary.*matches$" | grep -c ^

相关问题