From 96cbe04c2a2718f7f67a2e979095c06faaaea146 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 4 Jun 2020 11:45:21 -0500 Subject: [PATCH] Add Use Regex Pattern Matching With Grep as a unix til --- README.md | 3 +- unix/use-regex-pattern-matching-with-grep.md | 29 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 unix/use-regex-pattern-matching-with-grep.md diff --git a/README.md b/README.md index b5ebde5..a5f088f 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ and pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_921 TILs and counting..._ +_922 TILs and counting..._ --- @@ -882,6 +882,7 @@ _921 TILs and counting..._ - [Switch Versions of a Brew Formula](unix/switch-versions-of-a-brew-formula.md) - [Touch Access And Modify Times Individually](unix/touch-access-and-modify-times-individually.md) - [Undo Some Command Line Editing](unix/undo-some-command-line-editing.md) +- [Use Regex Pattern Matching With Grep](unix/use-regex-pattern-matching-with-grep.md) - [View A Web Page In The Terminal](unix/view-a-web-page-in-the-terminal.md) - [Watch The Difference](unix/watch-the-difference.md) - [Watch This Run Repeatedly](unix/watch-this-run-repeatedly.md) diff --git a/unix/use-regex-pattern-matching-with-grep.md b/unix/use-regex-pattern-matching-with-grep.md new file mode 100644 index 0000000..e20f1a4 --- /dev/null +++ b/unix/use-regex-pattern-matching-with-grep.md @@ -0,0 +1,29 @@ +# Use Regex Pattern Matching With Grep + +The `grep` command supports perl-flavored regular expression pattern matching. +Rather than grepping for specific words, you can use regex with grep to find +patterns throughout a text or command output. + +As an example, I can list all Ruby versions available for install with +[`asdf`](https://github.com/asdf-vm/asdf) using the following command. + +```bash +$ asdf list-all ruby +``` + +This produces a ton of lines of output including versions of `jruby` and +`truffleruby`. + +I can use grep to filter this list down to the MRI versions which all start +with a digit (e.g. `2.6.5`). + +```bash +$ asdf list-all ruby | grep "^[[:digit:]]" +``` + +That regex says, find all lines that begin (`^`) with a number (`[[:digit:]]`). +This means grep will filter down the output to things like `1.9.3-p551`, +`2.6.5`, and `2.7.0-preview2` whereas it will exclude `truffleruby-19.0.0` and +`jruby-9.2.9.0`. + +[source](https://www.digitalocean.com/community/tutorials/using-grep-regular-expressions-to-search-for-text-patterns-in-linux)