grep的递归和计数 [英] Grep Recursive and Count

查看:149
本文介绍了grep的递归和计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要搜索有很多子目录的目录中的文件里面的字符串:

Need to search a directories with lots of sub-directories for a string inside files:

我使用的是:

grep -c -r "string here" *

我如何发现总的算?

How can I total count of finds?

我怎么能输出到至少一个实例文件只有那些文件?

How can I output to file only those files with at least one instance?

推荐答案

这对我的作品(它会在每个文件中找到字符串这里的总数)。然而,它不显示总为检索所有文件。这里是你如何能得到它:

It works for me (it gets the total number of 'string here' found in each file). However, it does not display the total for ALL files searched. Here is how you can get it:

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

该列表将在出总将被发送到stdout。

The list will be in out and the total will be sent to STDOUT.

下面是关于Python2.5.4目录树的输出:

Here is the output on the Python2.5.4 directory tree:

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

如果你只是想获得与字符串出现线,改成这样:

If you just want to get lines with occurrences of 'string', change to this:

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)计数如何被打印出来。

You can change how the files ($1) and the count per file ($2) is printed.

这篇关于grep的递归和计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆