Featured image of post SED in a Nutshell

SED in a Nutshell

SED in a Nutshell

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:

1
sed 's/old-text/new-text/g' filename
  • s → Stands for substitute.
  • old-text → The text to find.
  • new-text → The text to replace it with.
  • gGlobal, meaning replace all occurrences in a line.
  • filename → The file to process.

Let’s see it in action:

1
echo "I love Vi" | sed 's/Vi/Vim/'

Output:

1
I love Vim

Boom. Just like that, history is corrected.


Common sed Use Cases

1. Replacing Text in a File

1
sed -i 's/cat/dog/g' animals.txt

Replaces every instance of “cat” with “dog” in animals.txt.

2. Deleting Lines

1
sed '3d' file.txt

Deletes the third line from file.txt.

1
sed '/error/d' logs.txt

Deletes all lines containing the word “error” in logs.txt.

3. Printing Specific Lines

1
sed -n '5p' file.txt

Prints only the 5th line.

1
sed -n '10,20p' file.txt

Prints lines 10 to 20.

4. Insert Text at the Beginning of a Line

1
sed 's/^/Hello: /' names.txt

Adds “Hello: " to the beginning of each line.

5. Insert Text at the End of a Line

1
sed 's/$/ Goodbye!/' greetings.txt

Adds “Goodbye!” to the end of each line.

6. Replace Only on a Specific Line

1
sed '3s/apple/orange/' fruits.txt

Replaces “apple” with “orange” only on line 3.

7. Replace Multiple Words

1
sed -e 's/dog/cat/g' -e 's/bird/fish/g' animals.txt

Replaces “dog” with “cat” and “bird” with “fish” in one command.

8. Remove Blank Lines

1
sed '/^$/d' file.txt

Deletes all empty lines.

9. Find & Replace Using Regex

1
sed 's/[0-9]\+/###/g' numbers.txt

Replaces all numbers with “###”.

10. Convert Lowercase to Uppercase

1
sed 's/[a-z]/\U&/g' text.txt

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

1
grep "error" logs.txt | sed 's/error/ERROR/g'

Finds “error” lines in logs.txt and replaces “error” with “ERROR”.

2. Using sed with awk

1
awk '{print $2}' data.txt | sed 's/foo/bar/g'

Extracts the second column from data.txt and replaces “foo” with “bar”.

3. Using sed in Scripts

1
2
#!/bin/bash
sed 's/apple/orange/g' fruits.txt > new_fruits.txt

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

  1. Wikipedia on sed: https://en.wikipedia.org/wiki/Sed
  2. GNU Sed Manual: https://www.gnu.org/software/sed/manual/sed.html
  3. Sed One-Liners: https://sed.sourceforge.io/sed1line.txt