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
grep file1 file2 --options