/* Code-Comments */

Sed: Grep's Successor For Substitution

In Git Log Archeology/ Digging In With Grep And Pickaxe, I noted that the grep utility is an acronym for ”globally search for a regular epression and print.” This fact helps explain why it is sometimes written as g/re/p.1

Then this morning, I saw a commit which included a Sed command that seemed like magic! I wanted to understand a bit more about it. What’s follow is a teaser at best with some basic Sed utilization.

At it’s most basic, Sed is a related utility program. Instead of simply searching, Sed is a stream editor that “parses and transforms text.”2

While Sed can do more, the most common use of it is to substitute text read in a stream.

$ echo day | sed 's/day/night'
night

Sed is also line oriented. Simply put this means that each line is processed independently.

Imagine the file:

"panagram"
The quick brown fox jumps over the lazy dog.
The lazy dog chases two lazy squirrels.
The really lazy squirrel gets caught. The merely lazy gets away.

And now, if we want to replace lazy with quick-witted we could do it as follows:

sed 's/lazy/quick-witted/' < panagram
The quick brown fox jumps over the quick-witted dog.
The quick-witted dog chases two lazy squirrels.
The really quick-witted squirrel gets caught. The merely lazy gets away.

Of course, this only substituted the first instance of lazy in each line.

If we really wanted all instances to be replaced, we would use /g to indicate a global replace. Again, Sed is line oriented meaning that the “universe” of each operation is a single line.

$ sed 's/lazy/quick-witted/g' < panagram
The quick brown fox jumps over the quick-witted dog.
The quick-witted dog chases two quick-witted squirrels.
The really quick-witted squirrel gets caught. The merely quick-witted gets away.

To keep learning more (like I need to), I went looking for reference materials that would help me understand (more than merely reading the spec). One of the most complete tutorials I’ve found for learning Sed is Bruce Barnett’s Sed - An Introduction and Tutorial by Bruce Barnett

Footnotes


Thanks for reading! My name's Stephen Weiss. I live in Chicago with my wife, Kate, and dog, Finn.
Click here to see the archives of my weeks in review and sign up yourself!