Count all occurrences of a string in lots of files with grep

前端 未结 15 1557
广开言路
广开言路 2020-11-28 01:07

I have a bunch of log files. I need to find out how many times a string occurs in all files.

grep -c string *

returns

...
f         


        
相关标签:
15条回答
  • 2020-11-28 01:44
    cat * | grep -c string
    
    0 讨论(0)
  • 2020-11-28 01:47

    Here is a faster-than-grep AWK alternative way of doing this, which handles multiple matches of <url> per line, within a collection of XML files in a directory:

    awk '/<url>/{m=gsub("<url>","");total+=m}END{print total}' some_directory/*.xml
    

    This works well in cases where some XML files don't have line breaks.

    0 讨论(0)
  • 2020-11-28 01:48
    grep -oh string * | wc -w
    

    will count multiple occurrences in a line

    0 讨论(0)
提交回复
热议问题