Unix Power ToolsUnix Power ToolsSearch this book

Chapter 34. The sed Stream Editor

Contents:

sed Sermon^H^H^H^H^H^HSummary
Two Things You Must Know About sed
Invoking sed
Testing and Using a sed Script: checksed, runsed
sed Addressing Basics
Order of Commands in a Script
One Thing at a Time
Delimiting a Regular Expression
Newlines in a sed Replacement
Referencing the Search String in a Replacement
Referencing Portions of a Search String
Search and Replacement: One Match Among Many
Transformations on Text
Hold Space: The Set-Aside Buffer
Transforming Part of a Line
Making Edits Across Line Boundaries
The Deliberate Scrivener
Searching for Patterns Split Across Lines
Multiline Delete
Making Edits Everywhere Except...
The sed Test Command
Uses of the sed Quit Command
Dangers of the sed Quit Command
sed Newlines, Quoting, and Backslashes in a Shell Script

34.1. sed Sermon^H^H^H^H^H^HSummary

^H^H^H are ASCII backspace characters. Written printably in email and Usenet messages, they're a tongue-in-cheek way of "erasing" the characters before without actually erasing them. They let you say "I didn't want you to see that" when you actually do.

sed (stream editor) amazes me. Why? It's not just that sed can edit data as it streams through a pipe (like all well-behaved Unix filters (Section 1.5) do). sed can test and branch and substitute and hold and exchange data as it streams through, but so can almost any scripting language. Maybe it's the minimalist in me that loves a tiny program (by today's standards, at least) with just a few operations -- but operations so well-chosen that they make the tool powerful for its size. Sure, sure, Perl probably can do everything that sed can -- and do each of those things in twenty different ways. Ah, I've got it: when I'm trying to do anything more than a simple substitution on data streaming by, sed's elegant simplicity almost forces me to strip a problem to its basics, to think of what I really need to do. No functions, no libraries, nothing except beautifully simple functionality.

[As someone who learned Perl regular expressions before I learned sed, I can relate to what Jerry is saying. One of the things I like about the classic Unix toolbox programs like sed is that they really do force you into a sort of Shaker-like elegant simplicity; the best programs, no matter what the language, have a quality like a Shaker chair: pure function, but with a respect for the fact that function doesn't have to be ugly. -- SJC]

End of sermon. ;-) Even if you aren't into elegance and simplicity, and you just wanna get the job done, what do we cover about sed that might be useful?

In this chapter, we start out with the basics: Section 34.2, Section 34.3, Section 34.4, Section 34.5, Section 34.6, and Section 34.7 show you how to get started, how to test your scripts, and how to structure more advanced scripts. Section 34.8 through Section 34.14 cover regular expressions and complex transformations. Section 34.15 through Section 34.24 deal with advanced topics such as multiline matching and deletions, tests, and exiting a script when you're done.

--JP and SJC



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.