0
sed -E 's/(^|[^-])--([^-]|$)/\1xx\2/g'

I am finding this command difficult to understand i cant even find the -E option under man sed, yet the command works. The substitution doesn't make sense as it looks at the beginning of the stream and then appears to immediately pipe.

Any explanation will be greatly appreciated.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
eb0t
  • 127
  • 1
  • 11

2 Answers2

2

It simply replaces -- with xx whenever it isn't part of a sequence of three or more dashes. The | isn't a pipe, it's an alternative. (^|[^-]) means "either the beginning of the line, or some character that isn't -", and ([^-]|$) means "either some character that isn't -, or the end of the line". Whatever is captured by those groups is then included in the output, unchanged, because we only wanted to match them for context anyway.

hobbs
  • 223,387
  • 19
  • 210
  • 288
0

Not sure which implementation of sed you're using.

Take a look at the gnu implementation.

reference: https://www.gnu.org/software/sed/manual/sed.txt

'-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.  *Note
    Extended regular expressions: ERE syntax.
Christian Will
  • 1,529
  • 3
  • 17
  • 25