SED in a Nutshell
Introduction
Ah, sed
. The little text-processing tool that has been around since dinosaurs roamed the early UNIX systems.
If you’ve ever found yourself drowning in a sea of text files, manually editing each one like some kind of medieval scribe, then sed
is about to become your best friend.
SED stands for Stream Editor.
It’s a command-line tool for parsing and transforming text. Think of it as “Find and Replace” on steroids—except instead of clicking buttons, you’re wielding the power of regular expressions like a wizard.
This article will take you through a whirlwind tour of sed
, from its history to common use cases.
A Brief History of sed
sed
was created in 1973 by Lee E. McMahon at Bell Labs as part of early UNIX development. It was inspired by the ed
text editor (hence the name Stream EDitor). Unlike ed
, which was interactive, sed
was designed to process text automatically, making it perfect for scripting and automation.
Fast forward a few decades, and sed
has become a staple of Linux, macOS, and other UNIX-like operating systems.
It’s so ingrained in UNIX culture that if you don’t know sed
, they don’t even let you into the secret sysadmin club (kidding… mostly).
For the history buffs, here’s the Wikipedia page on sed
:
https://en.wikipedia.org/wiki/Sed
Why Use sed
?
So why should you care about sed
? Because:
- It’s fast:
sed
can process large files in milliseconds. - It’s scriptable: Perfect for automation and batch processing.
- It’s regex-powered: Regular expressions let you manipulate text like a sorcerer.
- It saves time: No more manually editing files.
Basically, if you work with text files, logs, or configuration files, sed
can make your life easier.
sed
Syntax Basics
Before we dive into examples, let’s break down the basic sed
syntax:
|
|
s
→ Stands for substitute.old-text
→ The text to find.new-text
→ The text to replace it with.g
→ Global, meaning replace all occurrences in a line.filename
→ The file to process.
Let’s see it in action:
|
|
Output:
|
|
Boom. Just like that, history is corrected.
Common sed
Use Cases
1. Replacing Text in a File
|
|
Replaces every instance of “cat” with “dog” in animals.txt
.
2. Deleting Lines
|
|
Deletes the third line from file.txt
.
|
|
Deletes all lines containing the word “error” in logs.txt
.
3. Printing Specific Lines
|
|
Prints only the 5th line.
|
|
Prints lines 10 to 20.
4. Insert Text at the Beginning of a Line
|
|
Adds “Hello: " to the beginning of each line.
5. Insert Text at the End of a Line
|
|
Adds “Goodbye!” to the end of each line.
6. Replace Only on a Specific Line
|
|
Replaces “apple” with “orange” only on line 3.
7. Replace Multiple Words
|
|
Replaces “dog” with “cat” and “bird” with “fish” in one command.
8. Remove Blank Lines
|
|
Deletes all empty lines.
9. Find & Replace Using Regex
|
|
Replaces all numbers with “###”.
10. Convert Lowercase to Uppercase
|
|
Changes all lowercase letters to uppercase.
Combining sed
with Other Commands
Since sed
is a UNIX command, you can chain it with other tools:
1. Using sed
with grep
|
|
Finds “error” lines in logs.txt
and replaces “error” with “ERROR”.
2. Using sed
with awk
|
|
Extracts the second column from data.txt
and replaces “foo” with “bar”.
3. Using sed
in Scripts
|
|
Runs sed
inside a Bash script.
Wrapping Up
sed
is a powerhouse tool that every Linux/UNIX user should master. Whether you’re editing config files, processing logs, or just having fun replacing words, sed
has got your back.
If you’ve made it this far, congratulations! You now know more sed
than most people ever will. Go forth and automate!
Key Ideas
sed
is a Stream Editor used for text processing.- Developed in 1973 at Bell Labs for UNIX.
- Common use cases include search & replace, deleting lines, and inserting text.
- Uses regular expressions for powerful text manipulation.
- Can be combined with other UNIX tools like grep, awk, and bash scripts.
References
- Wikipedia on
sed
: https://en.wikipedia.org/wiki/Sed - GNU Sed Manual: https://www.gnu.org/software/sed/manual/sed.html
- Sed One-Liners: https://sed.sourceforge.io/sed1line.txt