4

In bash, as I understand it, I can use characters like . & ^ * in regular expressions with sed, but the -r option changes the nature of how regular expressions are, uh, expressed, kinda like grep vs. grep -E. But I can't find any summary of exactly HOW the syntax changes. Is there a list somewhere? Am I being naive in thinking this is the kind of thing that it ought to be possible to summarize in a table that could be printed on a couple of pages?

Do the characters that work with plain old non-extended sed regex expressions, still work the same way with the -r option? In other words are expressions that are valid WITHOUT the -r option, still valid, and still mean the same thing, WITH the -r option? Like they were a subset of the expressions valid WITH the -r option?

I keep thinking there must be a pithy summary of the difference with examples somewhere.

2
  • 1
    FYI on MacOS the option for sed is -E. If you want cross-platform scripts, do not use this option May 12, 2018 at 17:09
  • The only difference between basic and extended regular expressions is in the behavior of a few characters: ‘?’, ‘+’, parentheses, and braces (‘{}’). See Extended regular expressions
    – Noam Manos
    Jul 11, 2018 at 12:37

1 Answer 1

5

According to info sed, Extended regexps are those that 'egrep' accepts; they can be clearer because they usually have less backslashes, but are a GNU extension and hence scripts that use them are not portable. egrep being a synonym for grep -E.

This is indeed the case: without:

echo "abcdef" | sed 's/\([cd]\+\)/\U\1/'
abCDef

With:

echo "abcdef" | sed -r 's/([cd]+)/\U\1/'
abCDef

Some expressions may be valid with both, but in many cases they will be interpreted differently. The character escaping logic in regular, POSIX-compliant sed totally escapes me.

3
  • @ xenoid Thanks, I forgot about "info". I'll look at that and man and info for grep and egrep & get back to this after some study. So you're saying valid regexes for sed sans -r are NOT a subset of those valid for sed -r? Jul 10, 2017 at 17:28
  • Yes, they are different (they basically change the characters to escape)
    – xenoid
    Jul 10, 2017 at 17:54
  • OK, studied it. Got it. Or at least enough of it for now. There are pretty good summaries in info sed and info grep. It looks like there are at least some situations where you can do things with extended that you can't with, ahem, "regular" regular expressions, but not vice versa. So, I'm going to try to make a habit of using grep with -E and sed with -r from now on, and learn that syntax more thoroughly. I don't really care about posix. Jul 10, 2017 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.