Skip to content

Instantly share code, notes, and snippets.

@joshschmelzle
Created December 20, 2016 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshschmelzle/c2daf877499f3c6a94f5af5a2df490d0 to your computer and use it in GitHub Desktop.
Save joshschmelzle/c2daf877499f3c6a94f5af5a2df490d0 to your computer and use it in GitHub Desktop.
Grep only count occurrences

grep -ro "pattern to find in files" "directory to recursively search" | grep -c "pattern to find in files"

This solution will count all occurrences even if there are multiple on one line.

-r recursively searches the directory

-o will "show only the part of a line matching PATTERN" 

this is what splits up multiple occurences on a single line and makes grep print each match on a new line;

then pipe those newline-separated-results back into grep with -c to count the number of occurrences using the same pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment