-1

Is there a way for sed to pipe a capture group to another program, making \1 in the regexp's RHS equal to the output of that program?

For example, running

sed 's/lorem ipsum \(foobar\)/\1/g' file.txt

would pipe "foobar" through another program (e.g., tr 'o' 'a') to make \1 be "faabar" for sed to replace "lorem ipsum foobar" with "lorem ipsum faabar"?

This is just a simple example. I realize I could convert "foobar" to "faabar" without using tr.

Geremia
  • 2,736
  • 25
  • 31
  • Post input and expected output sample. It may be solved other way that you think. – Jotne Oct 27 '19 at 05:36
  • @Jotne I've added a fuller working example. – Geremia Oct 27 '19 at 06:00
  • GNU `sed` has an `/x` option; but what you are trying is wrong on so many levels. Use a tool with a proper HTML parser. Your code could rather easily be reimplemented in Perl but then doesn't PHP have a facility for that too (let alone one for reading a file line by line and applying a regex. Not that I'd recommend PHP for anything)? – tripleee Oct 27 '19 at 06:24
  • There's a famous joke about trying to parse HTML with regex, but [we are not supposed to link to it.](https://meta.stackoverflow.com/questions/261561/please-stop-linking-to-the-zalgo-anti-cthulhu-regex-rant) – tripleee Oct 27 '19 at 06:28
  • 3
    Possible duplicate of [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – tripleee Oct 27 '19 at 06:30

1 Answers1

0

Use the execute option:

e
This command allows one to pipe input from a shell command into pattern space. If a substitution was made, the command that is found in pattern space is executed and pattern space is replaced with its output. A trailing newline is suppressed; results are undefined if the command to be executed contains a NUL character. This is a GNU sed extension.

(source; original source)

Geremia
  • 2,736
  • 25
  • 31