【问题标题】:Grep Recursive and CountGrep 递归和计数
【发布时间】:2010-10-27 10:37:02
【问题描述】:

需要在包含大量子目录的目录中搜索文件中的字符串:

我正在使用:

grep -c -r "string here" *

我如何计算找到的总数?

如何仅将具有至少一个实例的文件输出到文件?

【问题讨论】:

  • 您能否提供更多详细信息,例如究竟是什么不起作用? grep 的完整路径是什么?你在什么系统上运行它?
  • 你能澄清一下你看到的输出是什么,你想要的和你得到的有什么不同吗?

标签: linux bash shell scripting


【解决方案1】:

我会尝试结合使用 find 和 grep。

find . | xargs grep -c "string here"

无论如何,grep -c -r "string here" * 适合我 (Mac OS X)。

【讨论】:

  • 注意文件名中的空格。考虑 -print0 和 -0
【解决方案2】:

使用 Bash 的进程替换,这给出了我认为你想要的输出? (如果不是,请澄清问题。)

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

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

【讨论】:

    【解决方案3】:

    它对我有用(它获取在每个文件中找到的“此处字符串”的总数)。但是,它不显示搜索的所有文件的总数。获取方法如下:

    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
    

    如果您只想获取出现“字符串”的行,请更改为:

    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) 的打印方式。

    【讨论】:

    • 你在哪里分配文本扩展?
    • 抱歉,这是来自以前的编辑。扩展真是太棒了:-)
    • 我怎样才能只输出到有字符串的文件。
    【解决方案4】:

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

    grep -r -l "your string here" .
    

    它将为每个匹配搜索表达式的文件输出一行文件名。

    【讨论】:

      【解决方案5】:

      使用 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
      

      结果中的颜色很好(--color=always 用于 grep),但在此处通过 awk 管道时它们会中断。所以最好不要启用它们,除非你想在之后让你的所有终端都上色:) 干杯!

      【讨论】:

        【解决方案6】:
        grep -rc "my string" ./ | grep :[1-9] >> file_name_by_count.txt
        

        像魅力一样工作。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-02-16
          • 1970-01-01
          • 1970-01-01
          • 2015-09-10
          • 2014-12-28
          • 2013-11-26
          • 2010-10-16
          相关资源
          最近更新 更多