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.sedis heavily used in Linux administration, DevOps, CI/CD pipelines, automation scripts, and production environments.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand how
sedworks - 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
sedin 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:
with
Opening the file manually isn't practical.
Instead:
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
- Transform
without modifying the original file unless requested.
Command Syntax¶
Sample File¶
Create:
Contents:
Display File¶
Equivalent to:
Print Specific Line¶
Display line 3.
Output:
Display lines 2–4.
Search and Replace¶
Replace first occurrence.
Output:
Only the first occurrence per line is replaced.
Global Replacement¶
Replace every occurrence.
The g flag means:
Global
Replace Only Line 2¶
Replace Between Lines¶
Ignore Case¶
Edit File In-Place¶
Normally:
does not change the file.
Modify the file permanently.
Create Backup Before Editing¶
Highly recommended.
Creates:
Delete Line¶
Delete line 3.
Delete lines 2–4.
Delete blank lines.
Insert Line¶
Insert before line 2.
Append Line¶
Append after line 2.
Replace Entire Line¶
Print Matching Lines¶
Delete Matching Lines¶
Using Regular Expressions¶
Replace all digits.
Remove extra spaces.
Replace multiple spaces with one.
Replace Tabs¶
Remove Trailing Spaces¶
Number Lines¶
Using Multiple Commands¶
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.
Update namespace.
Replace environment.
Update Terraform variables.
Modify NGINX config.
Kubernetes Example¶
Update image.
Docker Example¶
Replace base image.
CI/CD Example¶
GitLab pipeline.
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.
Task 2¶
Replace globally.
Task 3¶
Delete line 2.
Task 4¶
Print line 4.
Task 5¶
Insert header.
Task 6¶
Append footer.
Task 7¶
Edit file.
Task 8¶
Create backup.
Production Troubleshooting Scenario¶
Scenario
A company has 500 Kubernetes Deployment YAML files that still reference an old container image:
The new approved version is:
Update all deployment files safely while keeping backups.
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
sedwithgrep,find, andxargs.
Common Mistakes¶
❌ Editing production files without backups.
✅ Always use:
❌ Forgetting the g flag.
✅ Use:
Only replaces the first match on each line.
❌ Confusing sed with tr.
✅ - tr → Character replacement - sed → Pattern and line-based editing
Interview Questions¶
Beginner¶
- What does
sedstand for? - How do you replace text?
- What does
-ido? - What does the
gflag mean?
Intermediate¶
- Explain
sed -n. - Difference between
sedandtr. - How do you delete blank lines?
- How do you create backups before editing?
Architect Level¶
- How would you update thousands of Kubernetes manifests automatically?
- Why is
sedpreferred over manual editing in CI/CD pipelines? - 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¶
sedis a Stream Editor.- It edits text without opening a text editor.
- Use
-ifor in-place editing. - Use
-i.bakto create backups. gperforms global replacement.sedis 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.