|
| 1 | +# Count The Number Of ripgrep Pattern Matches |
| 2 | + |
| 3 | +If I run [`ripgrep`](https://github.com/BurntSushi/ripgrep) with a pattern |
| 4 | +against a project with many files, I may get a bunch of matches. So many |
| 5 | +matches even that they scroll off the screen. |
| 6 | + |
| 7 | +To get a summary of the number of matches in each file, I can include the `-c` |
| 8 | +flag: |
| 9 | + |
| 10 | +```bash |
| 11 | +❯ rg -c taco |
| 12 | +rails/parse-query-params-from-a-url.md:6 |
| 13 | +rails/params-is-a-hash-with-indifferent-access.md:4 |
| 14 | +ruby/fetch-warns-about-superseding-block-argument.md:1 |
| 15 | +ruby/add-comments-to-regex-with-free-spacing.md:1 |
| 16 | +ruby/create-a-csv-table-object.md:2 |
| 17 | +ruby/a-basic-case-statement.md:4 |
| 18 | +ruby/triple-equals-the-case-equality-operator.md:1 |
| 19 | +ruby/build-http-and-https-urls.md:4 |
| 20 | +rspec/check-specific-arguments-to-received-method.md:2 |
| 21 | +javascript/check-classes-on-a-dom-element.md:1 |
| 22 | +javascript/spread-merging-objects-includes-nil-values.md:2 |
| 23 | +xstate/custom-jest-matcher-for-xstate-machine-states.md:1 |
| 24 | +postgres/checking-inequality.md:1 |
| 25 | +case.rb:4 |
| 26 | +python/test-a-function-with-pytest.md:6 |
| 27 | +``` |
| 28 | + |
| 29 | +That is still a bunch of info and I may want to further summarize by getting a |
| 30 | +count of the total number of matches. I can do this by piping these results to |
| 31 | +an `awk` command that totals them up. |
| 32 | + |
| 33 | +```bash |
| 34 | +❯ rg -c taco | awk -F: '{total += $2} END {print total}' |
| 35 | +40 |
| 36 | +``` |
| 37 | + |
| 38 | +[Using `:` as the field |
| 39 | +seperator](https://www.gnu.org/software/gawk/manual/html_node/Full-Line-Fields.html), |
| 40 | +`awk` is able to get the number on the left side (`$2`) for each and sum that |
| 41 | +up. |
0 commit comments