mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add Count The Number Of ripgrep Pattern Matches as a Unix TIL
This commit is contained in:
@@ -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)
|
||||
|
||||
41
unix/count-the-number-of-ripgrep-pattern-matches.md
Normal file
41
unix/count-the-number-of-ripgrep-pattern-matches.md
Normal file
@@ -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.
|
||||
Reference in New Issue
Block a user