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

前端 未结 15 1504
广开言路
广开言路 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:24

    Obligatory AWK solution:

    grep -c string * | awk 'BEGIN{FS=":"}{x+=$2}END{print x}'
    

    Take care if your file names include ":" though.

    0 讨论(0)
  • 2020-11-28 01:28

    short recursive variant:

    find . -type f -exec cat {} + | grep -c 'string'
    
    0 讨论(0)
  • 2020-11-28 01:28

    Another oneliner using basic command line functions handling multiple occurences per line.

     cat * |sed s/string/\\\nstring\ /g |grep string |wc -l
    
    0 讨论(0)
  • 2020-11-28 01:30

    If you want number of occurrences per file (example for string "tcp"):

    grep -RIci "tcp" . | awk -v FS=":" -v OFS="\t" '$2>0 { print $2, $1 }' | sort -hr
    

    Example output:

    53  ./HTTPClient/src/HTTPClient.cpp
    21  ./WiFi/src/WiFiSTA.cpp
    19  ./WiFi/src/ETH.cpp
    13  ./WiFi/src/WiFiAP.cpp
    4   ./WiFi/src/WiFiClient.cpp
    4   ./HTTPClient/src/HTTPClient.h
    3   ./WiFi/src/WiFiGeneric.cpp
    2   ./WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino
    2   ./WiFiClientSecure/src/ssl_client.cpp
    1   ./WiFi/src/WiFiServer.cpp
    

    Explanation:

    • grep -RIci NEEDLE . - looks for string NEEDLE recursively from current directory (following symlinks), ignoring binaries, counting number of occurrences, ignoring case
    • awk ... - this command ignores files with zero occurrences and formats lines
    • sort -hr - sorts lines in reverse order by numbers in first column

    Of course, it works with other grep commands with option -c (count) as well. For example:

    grep -c "tcp" *.txt | awk -v FS=":" -v OFS="\t" '$2>0 { print $2, $1 }' | sort -hr
    
    0 讨论(0)
  • 2020-11-28 01:32
    cat * | grep -c string
    

    One of the rare useful applications of cat.

    0 讨论(0)
  • 2020-11-28 01:33

    The AWK solution which also handles file names including colons:

    grep -c string * | sed -r 's/^.*://' | awk 'BEGIN{}{x+=$1}END{print x}'
    

    Keep in mind that this method still does not find multiple occurrences of string on the same line.

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