Emacs, scripting and anything text oriented.

Search/replace groups using sed

Kaushal Modi

This is the most common way of my sed usage:

echo Good morning | sed 's/\(.*\s\+\).*/\1evening/g'

Here are brief notes on that ..

The above script changes Good morning to Good evening. Note that the following characters needed the escape character \:

  • Brackets to create regex groups: \( \)
  • Plus sign to match one or more times: \+
  • Note that the . and * characters don’t need escaping.