Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Post History

66%
+2 −0
Q&A Can I enter raw strings in fish to avoid escaping regexes for sed?

It is not the most elegant solution, but you may be able to use the string escape function of fish, as in: echo abc | sed -E (string escape 's/b+/X/') This would still escape the special charac...

posted 1y ago by AdminBee‭  ·  edited 1y ago by AdminBee‭

Answer
#4: Post edited by user avatar AdminBee‭ · 2023-06-21T09:33:53Z (over 1 year ago)
Add notice on correct syntax for GNU BRE extension as used in GNU `sed` without `-E`.
  • It is not the most elegant solution, but you may be able to use the [`string escape`](https://fishshell.com/docs/current/cmds/string-escape.html) function of `fish`, as in:
  • ```lang-shell
  • echo abc | sed -E (string escape 's/b+/X/')
  • ```
  • This would still escape the special characters, but in a "hidden" way - the user-visible RegEx is not cluttered with backslashes and the `sed` command sees the result of the command substitution, which is properly escaped.
  • So far, I don't know of any shell that has a feature to turn off interpretation of special characters (if anyone knows, it would likely be Stéphane Chazelas over at U&L SE).
  • **Note** that the `+` metacharacter is [extended regular expression syntax](https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended), which `sed` doesn't understand unless invoked with the `-E` option. When called "bare", it uses basic regular expressions, where the "one or more" would need to be expressed explicitly using range indicators, so you would need to write
  • ```
  • echo abc | sed (string escape 's/b\{1,\}/X/')
  • ```
  • where the `\` are now an actual part of the RegEx and therefore indispensable.
  • It is not the most elegant solution, but you may be able to use the [`string escape`](https://fishshell.com/docs/current/cmds/string-escape.html) function of `fish`, as in:
  • ```lang-shell
  • echo abc | sed -E (string escape 's/b+/X/')
  • ```
  • This would still escape the special characters, but in a "hidden" way - the user-visible RegEx is not cluttered with backslashes and the `sed` command sees the result of the command substitution, which is properly escaped.
  • So far, I don't know of any shell that has a feature to turn off interpretation of special characters (if anyone knows, it would likely be Stéphane Chazelas over at U&L SE).
  • **Note** that the `+` metacharacter is [extended regular expression syntax](https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended), which `sed` doesn't understand unless invoked with the `-E` option. When called "bare", it uses basic regular expressions, where the "one or more" would need to be expressed explicitly using range indicators, so you would need to write
  • ```lang-shell
  • echo abc | sed (string escape 's/b\{1,\}/X/')
  • ```
  • where the `\` are now an actual part of the RegEx and therefore indispensable.
  • **Update:** A comment by @Quasímodo notified me that the [GNU extension of BRE](https://www.gnu.org/software/sed/manual/sed.html#BRE-syntax) as used by GNU `sed` actually _does_ implement the `+` quantifier, but - similar to `{` and `}` - it must be given its special meaning by preceding it with a `\`. So in this case _the `\` is an integral part of the RegEx and must be literally passed to `sed` in the form `b\+`_ if you don't want to invoke it with `-E` to make use of ERE.
#3: Post edited by user avatar AdminBee‭ · 2023-06-20T15:29:43Z (over 1 year ago)
Correct strange typos
  • It is not the most elegant solution, but you may be able to use the [`string escape`](https://fishshell.com/docs/current/cmds/string-escape.html) function of `fish`, as in:
  • ```lang-shell
  • echo abc | sed -E (string escape 's/b+/X/')
  • ```
  • This would still escape the special characters, but in a "hidden" way - the user-visible RegEx is not cluttered with backslashes and the `sed` command sees the result of the command substitution, which is properly escaped.
  • So far, I don't know of any shell that has a feature to turn off interpretation of special characters (if anyone knows, it would likely be Stéphane Chazelas over at U&L SE).
  • **Note** that the `+` metacharacter is [extended regular expression syntax](https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended), which `sed` doesn't understand unless invoked with the `-E` option. When called "bare", it uses basic regular expressions, where the "one or more" would need to be expressed explicitly using range indicators, so you would need to write
  • ```
  • echo abc | sed (string escape 's/b\{1[](),\}/X/')
  • ```
  • where the `\` are now an actual part of the RegEx and therefore indispensable.
  • It is not the most elegant solution, but you may be able to use the [`string escape`](https://fishshell.com/docs/current/cmds/string-escape.html) function of `fish`, as in:
  • ```lang-shell
  • echo abc | sed -E (string escape 's/b+/X/')
  • ```
  • This would still escape the special characters, but in a "hidden" way - the user-visible RegEx is not cluttered with backslashes and the `sed` command sees the result of the command substitution, which is properly escaped.
  • So far, I don't know of any shell that has a feature to turn off interpretation of special characters (if anyone knows, it would likely be Stéphane Chazelas over at U&L SE).
  • **Note** that the `+` metacharacter is [extended regular expression syntax](https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended), which `sed` doesn't understand unless invoked with the `-E` option. When called "bare", it uses basic regular expressions, where the "one or more" would need to be expressed explicitly using range indicators, so you would need to write
  • ```
  • echo abc | sed (string escape 's/b\{1,\}/X/')
  • ```
  • where the `\` are now an actual part of the RegEx and therefore indispensable.
#2: Post edited by user avatar AdminBee‭ · 2023-06-20T15:28:04Z (over 1 year ago)
Add notice on why `+` wouldn't work with the `sed` invocation as-is
  • It is not the most elegant solution, but you may be able to use the [`string escape`](https://fishshell.com/docs/current/cmds/string-escape.html) function of `fish`, as in:
  • ```lang-shell
  • echo abc | sed (string escape 's/b+/X/')
  • ```
  • This would still escape the special characters, but in a "hidden" way - the user-visible RegEx is not cluttered with backslashes and the `sed` command sees the result of the command substitution, which is properly escaped.
  • So far, I don't know of any shell that has a feature to turn off interpretation of special characters (if anyone knows, it would likely be Stéphane Chazelas over at U&L SE).
  • It is not the most elegant solution, but you may be able to use the [`string escape`](https://fishshell.com/docs/current/cmds/string-escape.html) function of `fish`, as in:
  • ```lang-shell
  • echo abc | sed -E (string escape 's/b+/X/')
  • ```
  • This would still escape the special characters, but in a "hidden" way - the user-visible RegEx is not cluttered with backslashes and the `sed` command sees the result of the command substitution, which is properly escaped.
  • So far, I don't know of any shell that has a feature to turn off interpretation of special characters (if anyone knows, it would likely be Stéphane Chazelas over at U&L SE).
  • **Note** that the `+` metacharacter is [extended regular expression syntax](https://en.wikipedia.org/wiki/Regular_expression#POSIX_basic_and_extended), which `sed` doesn't understand unless invoked with the `-E` option. When called "bare", it uses basic regular expressions, where the "one or more" would need to be expressed explicitly using range indicators, so you would need to write
  • ```
  • echo abc | sed (string escape 's/b\{1[](),\}/X/')
  • ```
  • where the `\` are now an actual part of the RegEx and therefore indispensable.
#1: Initial revision by user avatar AdminBee‭ · 2023-06-20T14:37:11Z (over 1 year ago)
It is not the most elegant solution, but you may be able to use the [`string escape`](https://fishshell.com/docs/current/cmds/string-escape.html) function of `fish`, as in:
```lang-shell
echo abc | sed (string escape 's/b+/X/')
```
This would still escape the special characters, but in a "hidden" way - the user-visible RegEx is not cluttered with backslashes and the `sed` command sees the result of the command substitution, which is properly escaped.

So far, I don't know of any shell that has a feature to turn off interpretation of special characters (if anyone knows, it would likely be Stéphane Chazelas over at U&L SE).