Skip to content

Commit 85db06d

Browse files
committed
Add Count The Number Of ripgrep Pattern Matches as a Unix TIL
1 parent 96d4572 commit 85db06d

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1304 TILs and counting..._
13+
_1305 TILs and counting..._
1414

1515
---
1616

@@ -1235,6 +1235,7 @@ _1304 TILs and counting..._
12351235
- [Copying File Contents To System Paste Buffer](unix/copying-file-contents-to-system-paste-buffer.md)
12361236
- [Copying Nested Directories With Ditto](unix/copying-nested-directories-with-ditto.md)
12371237
- [Count The Number Of Matches In A Grep](unix/count-the-number-of-matches-in-a-grep.md)
1238+
- [Count The Number Of ripgrep Pattern Matches](unix/count-the-number-of-ripgrep-pattern-matches.md)
12381239
- [Create A File Descriptor with Process Substitution](unix/create-a-file-descriptor-with-process-substitution.md)
12391240
- [Create A Sequence Of Values With A Step](unix/create-a-sequence-of-values-with-a-step.md)
12401241
- [Curl With Cookies](unix/curl-with-cookies.md)

Diff for: unix/count-the-number-of-ripgrep-pattern-matches.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

Comments
 (0)