number_of_matches_per_file
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
number_of_matches_per_file [2021/10/31 17:48] – [References] admin | number_of_matches_per_file [2021/10/31 21:17] – [How it works] admin | ||
---|---|---|---|
Line 1: | Line 1: | ||
===== Task ===== | ===== Task ===== | ||
- | Given | + | Given a bunch of files in a directory, count the number of times a word occurs in each file. For example, given |
< | < | ||
% tail -n +1 * | % tail -n +1 * | ||
Line 17: | Line 18: | ||
</ | </ | ||
- | count the number of times the word foo occurred | + | count the number of occurrences of 'foo' |
< | < | ||
junk1.txt:4 | junk1.txt:4 | ||
Line 24: | Line 25: | ||
tags | Number of matches per file | tags | Number of matches per file | ||
+ | |||
+ | sample code demoes | cat with filename | ||
===== Solution using git grep and awk ===== | ===== Solution using git grep and awk ===== | ||
+ | If it is a git repository | ||
+ | < | ||
+ | git grep -o foo | awk -F':' | ||
+ | </ | ||
+ | |||
+ | If it is not a git repository | ||
< | < | ||
git grep --no-index -o foo | awk -F':' | git grep --no-index -o foo | awk -F':' | ||
Line 55: | Line 64: | ||
==== References ==== | ==== References ==== | ||
- | * https:// | + | * https:// |
==== tags ==== | ==== tags ==== | ||
- | awk frequency count | + | awk frequency count, awk count breakdown, uniq reverse output, "git grep" count matches, count "grep -o", "grep -o" counts, "grep -o" summarize |
- | ===== Solution using find and grep ===== | + | |
+ | ===== Solution using grep and awk ===== | ||
+ | < | ||
+ | grep -ro "came across" | ||
+ | </ | ||
+ | |||
+ | Useful if git is not available. | ||
+ | ===== Solution using find, grep and wc ===== | ||
< | < | ||
find * -printf 'echo " | find * -printf 'echo " | ||
Line 88: | Line 104: | ||
</ | </ | ||
- | Note: You have to use "grep -o" | + | Note: You have to use "grep -o" |
< | < | ||
Line 120: | Line 136: | ||
</ | </ | ||
- | ==== Reference | + | ==== References |
* https:// | * https:// | ||
number_of_matches_per_file.txt · Last modified: 2021/11/04 14:59 by admin