1

I am looking at a line of a file I opened with ed and displayed with .p:

First sentence. Another. One. Text continues

Now I want to replace the string First sentence. with replacement. However, the command

.s/.*\./replacement./

acts in a greedy manner, that is, it replaces First sentence. Another. One. by replacement, and not just First sentence. as desired.

How can I do a non-greedy replacement in ed?

  • 1
    This has been asked many times. Hint: non-greedy matches (like `.*?`) are your friends. Even better in this context is an exclusive character class, like `[^.]*`. – elixenide Jun 02 '17 at 06:21

1 Answers1

1

I'm not sure if ed supports non-greedy matching, but assuming it does not:

You want to match everything except a dot.

.s/[^.]*\./replacement./
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 1
    Be aware that `.` won't match across line breaks (it's *"every character but `\n`"*, really), while `[^.]` *will* of course happily match across line breaks. This might not be an issue with `ed` but it can lead to unexpected results in other contexts. – Tomalak Jun 02 '17 at 11:41