1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-04 23:58:01 +00:00

Add Use Regex Pattern Matching With Grep as a unix til

This commit is contained in:
jbranchaud
2020-06-04 11:45:21 -05:00
parent 26537a6717
commit 96cbe04c2a
2 changed files with 31 additions and 1 deletions

View File

@@ -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)

View File

@@ -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)