From c185ac18c59358558164708b2f2e3f66953525d0 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 23 Jun 2023 15:16:55 -0500 Subject: [PATCH] Add Find Occurrences Of Multiple Values With Ripgrep as a Unix TIL --- README.md | 3 ++- ...rrences-of-multiple-values-with-ripgrep.md | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 unix/find-occurrences-of-multiple-values-with-ripgrep.md diff --git a/README.md b/README.md index be5a9f2..583c87e 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). -_1318 TILs and counting..._ +_1319 TILs and counting..._ --- @@ -1269,6 +1269,7 @@ _1318 TILs and counting..._ - [Find Duplicate Lines In A File](unix/find-duplicate-lines-in-a-file.md) - [Find Files With fd](unix/find-files-with-fd.md) - [Find Newer Files](unix/find-newer-files.md) +- [Find Occurrences Of Multiple Values With Ripgrep](unix/find-occurrences-of-multiple-values-with-ripgrep.md) - [Fix Unlinked Node Binaries With asdf](unix/fix-unlinked-node-binaries-with-asdf.md) - [Forward Multiple Ports Over SSH](unix/forward-multiple-ports-over-ssh.md) - [Generate A SAML Key And Certificate Pair](unix/generate-a-saml-key-and-certificate-pair.md) diff --git a/unix/find-occurrences-of-multiple-values-with-ripgrep.md b/unix/find-occurrences-of-multiple-values-with-ripgrep.md new file mode 100644 index 0000000..edbf5de --- /dev/null +++ b/unix/find-occurrences-of-multiple-values-with-ripgrep.md @@ -0,0 +1,25 @@ +# Find Occurrences Of Multiple Values With Ripgrep + +Let's say I have a several values that show up throughout the files in my +project. They are `Valid`, `Restricted`, `Refunded`, `Disputed`, and `Banned`. + +I want to find all occurrences of each of these values. + +This can be done with [`rg` (ripgrep)](https://github.com/BurntSushi/ripgrep) +and a bit of regex. + +```bash +rg "\b(Valid|Restricted|Refunded|Disputed|Banned)\b" +``` + +This uses `\b` on both ends to indicate word boundaries. This ensures it +matches on `Valid` without also matching on `Validate`. It then wraps all the +options in parentheses separated by `|` which says, "match on this word, this +word, ..., or this word". + +I can even take this a step further by only matching on quoted instances of +these words like so: + +```bash +$ rg "[\"']\b(Valid|Restricted|Refunded|Disputed|Banned)\b[\"']" +```