Skip to content

Commit c185ac1

Browse files
committed
Add Find Occurrences Of Multiple Values With Ripgrep as a Unix TIL
1 parent 3c7899c commit c185ac1

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
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-
_1318 TILs and counting..._
13+
_1319 TILs and counting..._
1414

1515
---
1616

@@ -1269,6 +1269,7 @@ _1318 TILs and counting..._
12691269
- [Find Duplicate Lines In A File](unix/find-duplicate-lines-in-a-file.md)
12701270
- [Find Files With fd](unix/find-files-with-fd.md)
12711271
- [Find Newer Files](unix/find-newer-files.md)
1272+
- [Find Occurrences Of Multiple Values With Ripgrep](unix/find-occurrences-of-multiple-values-with-ripgrep.md)
12721273
- [Fix Unlinked Node Binaries With asdf](unix/fix-unlinked-node-binaries-with-asdf.md)
12731274
- [Forward Multiple Ports Over SSH](unix/forward-multiple-ports-over-ssh.md)
12741275
- [Generate A SAML Key And Certificate Pair](unix/generate-a-saml-key-and-certificate-pair.md)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Find Occurrences Of Multiple Values With Ripgrep
2+
3+
Let's say I have a several values that show up throughout the files in my
4+
project. They are `Valid`, `Restricted`, `Refunded`, `Disputed`, and `Banned`.
5+
6+
I want to find all occurrences of each of these values.
7+
8+
This can be done with [`rg` (ripgrep)](https://github.com/BurntSushi/ripgrep)
9+
and a bit of regex.
10+
11+
```bash
12+
rg "\b(Valid|Restricted|Refunded|Disputed|Banned)\b"
13+
```
14+
15+
This uses `\b` on both ends to indicate word boundaries. This ensures it
16+
matches on `Valid` without also matching on `Validate`. It then wraps all the
17+
options in parentheses separated by `|` which says, "match on this word, this
18+
word, ..., or this word".
19+
20+
I can even take this a step further by only matching on quoted instances of
21+
these words like so:
22+
23+
```bash
24+
$ rg "[\"']\b(Valid|Restricted|Refunded|Disputed|Banned)\b[\"']"
25+
```

0 commit comments

Comments
 (0)