Chapter 6. Processing Text with sed

When you need to edit a file, you typically open up your favorite editor, perform the change, and then save the file and exit. Editors are great for modifying files and seem to be suitable for any type of editing needed. However, imagine you have a web site with a couple of thousand HTML files that need the copyright year at the bottom changed from 2004 to 2005. The interactive nature of editors would require you to type every change that you need to make. You would launch your editor and open each file individually and, like an automaton, make the change, save, exit, repeat. After spending hours performing the same change on thousands of files, you realize you've forgotten about a whole section of the web site and actually have several thousand more, and next year you will need to do this again, with more files. There has to be a better way.

Fortunately, there is. This chapter introduces you to sed, an intelligent text-processing tool that will save you not only time but also, more important, your sanity. The sed command gives you the power to perform these changes on the command line, or in a shell script, with very little headache. Even better, sed will allow you to repeat the advanced batch editing of files simply. Sed can be run on the command line and is a powerful addition to any shell scriptwriter's toolbox. Learning the building blocks of sed will enable you to create tools to solve complex problems automatically and efficiently.

This chapter ...

Get Beginning Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.