From 85db06d05142ece6cfff896af3192493d96f30ea Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Wed, 24 May 2023 12:40:49 -0500 Subject: [PATCH] Add Count The Number Of ripgrep Pattern Matches as a Unix TIL --- README.md | 3 +- ...t-the-number-of-ripgrep-pattern-matches.md | 41 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 unix/count-the-number-of-ripgrep-pattern-matches.md diff --git a/README.md b/README.md index 8dc4dcd..15c61e3 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1304 TILs and counting..._ +_1305 TILs and counting..._ --- @@ -1235,6 +1235,7 @@ _1304 TILs and counting..._ - [Copying File Contents To System Paste Buffer](unix/copying-file-contents-to-system-paste-buffer.md) - [Copying Nested Directories With Ditto](unix/copying-nested-directories-with-ditto.md) - [Count The Number Of Matches In A Grep](unix/count-the-number-of-matches-in-a-grep.md) +- [Count The Number Of ripgrep Pattern Matches](unix/count-the-number-of-ripgrep-pattern-matches.md) - [Create A File Descriptor with Process Substitution](unix/create-a-file-descriptor-with-process-substitution.md) - [Create A Sequence Of Values With A Step](unix/create-a-sequence-of-values-with-a-step.md) - [Curl With Cookies](unix/curl-with-cookies.md) diff --git a/unix/count-the-number-of-ripgrep-pattern-matches.md b/unix/count-the-number-of-ripgrep-pattern-matches.md new file mode 100644 index 0000000..57b439c --- /dev/null +++ b/unix/count-the-number-of-ripgrep-pattern-matches.md @@ -0,0 +1,41 @@ +# Count The Number Of ripgrep Pattern Matches + +If I run [`ripgrep`](https://github.com/BurntSushi/ripgrep) with a pattern +against a project with many files, I may get a bunch of matches. So many +matches even that they scroll off the screen. + +To get a summary of the number of matches in each file, I can include the `-c` +flag: + +```bash +❯ rg -c taco +rails/parse-query-params-from-a-url.md:6 +rails/params-is-a-hash-with-indifferent-access.md:4 +ruby/fetch-warns-about-superseding-block-argument.md:1 +ruby/add-comments-to-regex-with-free-spacing.md:1 +ruby/create-a-csv-table-object.md:2 +ruby/a-basic-case-statement.md:4 +ruby/triple-equals-the-case-equality-operator.md:1 +ruby/build-http-and-https-urls.md:4 +rspec/check-specific-arguments-to-received-method.md:2 +javascript/check-classes-on-a-dom-element.md:1 +javascript/spread-merging-objects-includes-nil-values.md:2 +xstate/custom-jest-matcher-for-xstate-machine-states.md:1 +postgres/checking-inequality.md:1 +case.rb:4 +python/test-a-function-with-pytest.md:6 +``` + +That is still a bunch of info and I may want to further summarize by getting a +count of the total number of matches. I can do this by piping these results to +an `awk` command that totals them up. + +```bash +❯ rg -c taco | awk -F: '{total += $2} END {print total}' +40 +``` + +[Using `:` as the field +seperator](https://www.gnu.org/software/gawk/manual/html_node/Full-Line-Fields.html), +`awk` is able to get the number on the left side (`$2`) for each and sum that +up.