0

I want to search and replace the specified string with "module" in this line

ex. "apple" string in the line:

"happy applegg module banana tree" => "happy berrygg module banana tree"

sed 's/^((?=module).)*.*apple.*$/berry/g' filename >> newfile

^((?=module).)*$ => the positive look behind can fit the module, and .*apple.*$ can fit the apple

but it failed, how can I modify the sed command with regular expressions to do what I want?

beaver
  • 523
  • 1
  • 9
  • 20

1 Answers1

0

Sed does not support full regexps. It does not even support grouping with simple (...) -- you need backslash escapes. You can use Perl's "-p" option for sed-like pass through and s/.../.../ syntax and perl's full regex support.

Mischa
  • 2,240
  • 20
  • 18
  • Your definition of *"full"* is flawed. `sed`most definitely support "full" regular expressions, just not the dialect which was significantly extended and enhanced in Perl and subsequently implemented by a number of other languages. – tripleee Jan 15 '18 at 12:11
  • Are you thinking of some specific sed? Across Linux/Darwin/AIX/Solaris/HP-UX, I've yet to see one that supports (?...) ... And I just confirmed it with gnu sed 4.1.5, perl 5.8.8 on centos. – Mischa Jan 15 '18 at 17:32
  • You misunderstand me. "Full" regular expression support is an incorrect term for what you are describing. The dialect understood by `sed` indeed does not support lookarounds, but those are not required by any meaningful definition of "full" regular expressions. Technically, lookarounds extend the formalism to the point where the language of allowed expressions isn't actually [*regular*](https://en.wikipedia.org/wiki/Regular_language) at all. – tripleee Jan 16 '18 at 06:02