diff --git a/README.md b/README.md index d9bc8b9..da75715 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://tinyletter.com/jbranchaud). -_1095 TILs and counting..._ +_1096 TILs and counting..._ --- @@ -948,6 +948,7 @@ _1095 TILs and counting..._ - [Apply Multiple Substitutions To The Input](sed/apply-multiple-substitutions-to-the-input.md) - [Extract Value From Command Output With Sed](sed/extract-value-from-command-output-with-sed.md) - [Grab All The Method Names Defined In A Ruby File](sed/grab-all-the-method-names-defined-in-a-ruby-file.md) +- [OSX sed Does Regex A Bit Different](sed/osx-sed-does-regex-a-bit-different.md) - [Output Only Lines Involved In A Substitution](sed/output-only-lines-involved-in-a-substitution.md) - [Use An Alternative Delimiter In A Substitution](sed/use-an-alternative-delimiter-in-a-substitution.md) diff --git a/sed/osx-sed-does-regex-a-bit-different.md b/sed/osx-sed-does-regex-a-bit-different.md new file mode 100644 index 0000000..c5ef07d --- /dev/null +++ b/sed/osx-sed-does-regex-a-bit-different.md @@ -0,0 +1,32 @@ +# OSX sed Does Regex A Bit Different + +With GNU sed, `\+`, `\?`, `\(...\)` and friends are considered extended regex +characters. You can use them directly with the preceding backslashes. Or you +can include the `-r` flag to turn on extended regex and use them without. + +```bash +$ echo '11+1 = 12' | sed 's/1+/3/' +131 = 12 +$ echo '11+1 = 12' | sed -r 's/1+/3/' +3+1 = 12 +``` + +With OSX sed, `\+`, `\?`, and `\|` are not interpreted as part of the basic +regex. To use them at all you need to include `-E` to turn on extended regex. +The capture characters (`\(...\)`) are available with basic regex. + +```bash +# Basic, always treated as literal + +$ echo '11+1 = 12' | sed 's/1+/3/' +131 = 12 +$ echo '11+1 = 12' | sed 's/1\+/3/' +131 = 12 + +# Extended, + is now a meta-character +$ echo '11+1 = 12' | sed -E 's/1+/3/' +3+1 = 12 +$ echo '11+1 = 12' | sed -E 's/1\+/3/' +131 = 12 +``` + +[source](https://unix.stackexchange.com/a/131940/5916)