21

I'm working on some old code and I found that I used to use

sed -E 's/findText/replaceWith/g' #findText would contain a regex

but I now try

sed -e 's/findText/replaceWith/g'

It seems to do the same thing, or does it? I kinda remember there being a reason I done it but I can't remember and doing "man sed" doesn't help as they don't have anything about -E only -e that doesn't make much sense ether.

-e, --expression=script
  Append the editing commands in script to the end of 
  the editing command script. script may contain more 
  than one newline separated command.

I thought -e meant it would match with a regex...

GNU sed version 4.2.1
Mint
  • 14,388
  • 30
  • 76
  • 108

2 Answers2

40

From source code, -E is an undocumented option for compatibility with BSD sed.

/* Undocumented, for compatibility with BSD sed.  */
case 'E':
case 'r':
  if (extended_regexp_flags)
    usage(4);
  extended_regexp_flags = REG_EXTENDED;
  break;

And from manual, -E in BSD sed is used to support extended regular expressions.

czchen
  • 5,850
  • 2
  • 26
  • 17
1

From sed's documentation:

-E

-r

--regexp-extended

Use extended regular expressions rather than basic regular expressions. Extended regexps are those that egrep accepts; they can be clearer because they usually have fewer backslashes. Historically this was a GNU extension, but the -E extension has since been added to the POSIX standard (http://austingroupbugs.net/view.php?id=528), so use -E for portability. GNU sed has accepted -E as an undocumented option for years, and *BSD seds have accepted -E for years as well, but scripts that use -E might not port to other older systems. See Extended regular expressions.

Therefore it seems that -E should be the preferred way to declare that you are going to use (E)xtended regular expressions, rather than -r.

Instead, -e just specifies that what follows is the script that you want to execute with sed (something like 's/bla/abl/g').

Always from the documentation:

Without -e or -f options, sed uses the first non-option parameter as the script, and the following non-option parameters as input files.

Michele Piccolini
  • 2,634
  • 16
  • 29