Sed

In the days before GUIs, Unix users depended on a suite of command line tools to do their text manipulation. One of those tools, sed, is still used and useful today but many younger engineers are unfamiliar with it. The basic idea is that it will read each line of a file, perform a series of transformations on it, and write the result to stdout. Hence the name: stream editor.

If you’re a vi/vim user, the sed command structure will be familiar to you but even if you’re not, it’s easy to learn. Daniel Leary over at Theodo has a nice introduction to sed that shows how to perform a series of changes to a CSV file. It covers only a small part of sed’s functionality but that part is probably the most used and useful part.

Once you’ve read through Leary’s tutorial, you’ll be able to make basic changes to a file and read the sed man page to figure out the other functionality. Sed is one of those utilities that you—or at least I—don’t need that often but when you do, it’s just the thing. Emacs users will say, “But I can do that easily in Emacs,” which is true but what if you have to do it repeatedly, say on a daily basis? With sed you can record the commands in a file and then just run sed on a file as needed. It’s really very handy and worth spending a few minutes getting familiar with. Leary’s tutorial is a good place to start.

If you want more information, The Unix Programming Environment by Kernighan and Pike has an excellent section on sed that covers more of the details.

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