sed RE Substitution

For next time I forget…

sed -i .bk -E 's/text to replace (text to keep)/replaced text \1/' file
  • -i: edit the file rather than just stdout the result. A backup of the original is made using the supplied extension
  • -E: support modern regex, as opposed to old or basic regex. Without -E, you’ll have to escape the ()

Each instance of (something) will be treated as a variable which can be called with \#