2020-03-16
|~2 min read
|376 words
In Git Log Archeology/ Digging In With Grep And Pickaxe, I noted that the grep
utility is an acronym for “_g_lobally search for a _r_egular _e_pression and _p_rint.” 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 a file named panagram
:
cat 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
Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!