Skip to content

sed Command — Stream Editor for Text Processing and Automation

The sed (Stream Editor) command is one of the most powerful text-processing tools in Linux. It allows you to search, replace, insert, delete, transform, and manipulate text without opening a text editor. sed is heavily used in Linux administration, DevOps, CI/CD pipelines, automation scripts, and production environments.


Learning Path

Linux Mastery → Module 3: Text Processing → Lesson 16

Difficulty: Intermediate → Advanced

Reading Time: 60 Minutes

Course Progress

Course: Linux Mastery

Module: Text Processing

Lesson: 16 of 18


What You'll Learn

After completing this lesson, you'll be able to:

  • Understand how sed works
  • Print specific lines
  • Search and replace text
  • Replace globally
  • Edit files in-place
  • Delete lines
  • Insert and append lines
  • Replace using regular expressions
  • Process configuration files
  • Automate text transformations
  • Use sed in production DevOps workflows

Prerequisites

Complete:

  • Module 1
  • Module 2
  • Module 3 Lessons 1–15

Why Learn sed?

Imagine you have a configuration file containing 10,000 lines.

You need to replace:

dev.example.com

with

prod.example.com

Opening the file manually isn't practical.

Instead:

sed 's/dev.example.com/prod.example.com/' config.txt

Done instantly.


What is sed?

sed stands for:

Stream Editor

Unlike editors such as vim or nano, sed processes text line by line.

It can:

  • Search
  • Replace
  • Delete
  • Insert
  • Append
  • Print
  • Transform

without modifying the original file unless requested.


Command Syntax

sed [OPTIONS] 'COMMAND' FILE

Sample File

Create:

cat > employees.txt

Contents:

Alice Engineering

Bob HR

Charlie Finance

David DevOps

Eve Engineering

Display File

sed '' employees.txt

Equivalent to:

cat employees.txt

Print Specific Line

Display line 3.

sed -n '3p' employees.txt

Output:

Charlie Finance

Display lines 2–4.

sed -n '2,4p' employees.txt

Search and Replace

Replace first occurrence.

sed 's/Engineering/Cloud/' employees.txt

Output:

Alice Cloud

Bob HR

Charlie Finance

David DevOps

Eve Engineering

Only the first occurrence per line is replaced.


Global Replacement

Replace every occurrence.

sed 's/Linux/Ubuntu/g'

The g flag means:

Global


Replace Only Line 2

sed '2 s/HR/Human-Resources/' employees.txt

Replace Between Lines

sed '2,4 s/DevOps/SRE/'

Ignore Case

sed 's/linux/ubuntu/i'

Edit File In-Place

Normally:

sed 's/HR/Human Resources/' employees.txt

does not change the file.

Modify the file permanently.

sed -i 's/HR/Human Resources/' employees.txt

Create Backup Before Editing

Highly recommended.

sed -i.bak 's/HR/Human Resources/' employees.txt

Creates:

employees.txt

employees.txt.bak

Delete Line

Delete line 3.

sed '3d' employees.txt

Delete lines 2–4.

sed '2,4d' employees.txt

Delete blank lines.

sed '/^$/d' file.txt

Insert Line

Insert before line 2.

sed '2i\
Department List
' employees.txt

Append Line

Append after line 2.

sed '2a\
Cloud Team
' employees.txt

Replace Entire Line

sed '3c\
Charlie Security
' employees.txt

Print Matching Lines

sed -n '/Engineering/p' employees.txt

Delete Matching Lines

sed '/HR/d' employees.txt

Using Regular Expressions

Replace all digits.

sed 's/[0-9]/X/g'

Remove extra spaces.

sed 's/  */ /g'

Replace multiple spaces with one.


Replace Tabs

sed 's/\t/ /g'

Remove Trailing Spaces

sed 's/[[:space:]]*$//'

Number Lines

sed = employees.txt

Using Multiple Commands

sed -e 's/Linux/Ubuntu/' \
    -e 's/Docker/Podman/'

Common sed Options

Option Description
-n Suppress automatic printing
-i Edit file in place
-e Multiple expressions
-f Read commands from file

Real Production Examples

Replace image tag.

sed -i 's/v1.2.0/v1.3.0/' deployment.yaml

Update namespace.

sed -i 's/dev/prod/' deployment.yaml

Replace environment.

sed -i 's/ENV=dev/ENV=prod/'

Update Terraform variables.

sed -i 's/us-central1/us-east1/'

Modify NGINX config.

sed -i 's/listen 80/listen 8080/'

Kubernetes Example

Update image.

sed -i 's/nginx:1.24/nginx:1.27/' deployment.yaml

Docker Example

Replace base image.

sed -i 's/python:3.10/python:3.12/'

CI/CD Example

GitLab pipeline.

sed -i "s/IMAGE_TAG/$CI_COMMIT_SHA/"

Production Perspective

Every DevOps engineer uses sed.

Typical use cases:

  • CI/CD
  • Configuration management
  • YAML editing
  • Log cleanup
  • Kubernetes manifests
  • Terraform variables
  • Helm templates
  • Automation scripts

Hands-on Lab

Task 1

Replace Engineering.

sed 's/Engineering/Cloud/' employees.txt

Task 2

Replace globally.

sed 's/e/E/g'

Task 3

Delete line 2.

sed '2d' employees.txt

Task 4

Print line 4.

sed -n '4p' employees.txt

Task 5

Insert header.

sed '1i\
Employee Report
'

Task 6

Append footer.

sed '$a\
End of Report
'

Task 7

Edit file.

sed -i 's/HR/Human Resources/'

Task 8

Create backup.

sed -i.bak 's/DevOps/SRE/'

Production Troubleshooting Scenario

Scenario

A company has 500 Kubernetes Deployment YAML files that still reference an old container image:

image: nginx:1.24

The new approved version is:

image: nginx:1.27

Update all deployment files safely while keeping backups.

find . -name "*.yaml" -print0 | \
xargs -0 sed -i.bak 's/nginx:1.24/nginx:1.27/g'

Performance Tip

Use sed for stream processing.

It processes files line by line without loading the entire file into memory, making it suitable for very large log files and configuration files.


Best Practices

  • Always test before using -i.
  • Create backups with -i.bak.
  • Use regular expressions carefully.
  • Validate YAML/JSON after modifications.
  • Combine sed with grep, find, and xargs.

Common Mistakes

❌ Editing production files without backups.

✅ Always use:

sed -i.bak

❌ Forgetting the g flag.

✅ Use:

sed 's/a/b/'

Only replaces the first match on each line.


❌ Confusing sed with tr.

✅ - tr → Character replacement - sed → Pattern and line-based editing


Interview Questions

Beginner

  1. What does sed stand for?
  2. How do you replace text?
  3. What does -i do?
  4. What does the g flag mean?

Intermediate

  1. Explain sed -n.
  2. Difference between sed and tr.
  3. How do you delete blank lines?
  4. How do you create backups before editing?

Architect Level

  1. How would you update thousands of Kubernetes manifests automatically?
  2. Why is sed preferred over manual editing in CI/CD pipelines?
  3. How would you safely update configuration files across hundreds of servers?

Summary

In this lesson, you learned:

  • Search and replace
  • Global replacement
  • In-place editing
  • Line deletion
  • Line insertion
  • Appending text
  • Regular expressions
  • Production automation

sed is one of the most important Linux commands for automation and configuration management. It is used extensively in DevOps, cloud engineering, CI/CD, and system administration.


Key Takeaways

  • sed is a Stream Editor.
  • It edits text without opening a text editor.
  • Use -i for in-place editing.
  • Use -i.bak to create backups.
  • g performs global replacement.
  • sed is indispensable for DevOps and automation.

What's Next?

awk Command — The Ultimate Linux Text Processing Language

In the next lesson, you'll master field-based processing, calculations, reporting, filtering, and advanced text manipulation using awk.