Batch Editing with ed

I’ve written before about the ed editor and the faithful who still use it today. A tweet by Mike Zamansky lead me to this Julia Evans post on batch editing files with ed. Evans has a typical editing problem: she wants to duplicate each line in several files that contain a certain string but also change the string to something else on the new line. See her post for the details.

She chose to do this by writing an ed script to make the change and then call ed with the script (presumably) as part of a larger shell script that does this for each file. My first thought was that this made sense even today because ed scripts have always been used in just this way and it’s still an easy and elegant solution. But then I got to wondering how I’d perform that task.

My inclination was to try some sort of Emacs trick, of course. Evans is a Vim user so she doesn’t have the awesome power of Emacs at her fingertips and can be forgiven for falling back to ed. Since we haven’t had an Emacs puzzle for a while, here’s a challenge: figure out how you’d use Emacs to get this done.

Zamansky, being less inclined to rush in, offers the following excellent Sed solution:

It’s probably hard to beat that solution even with Emacs but see what you can do.

This entry was posted in General and tagged , . Bookmark the permalink.