Guide to Mastering the sed Command in Linux: Quick Text Editing
sed (Stream Editor) is a powerful command line tool for editing and converting text in Linux.
It allows you to search, replace, delete or add a file by reading it line by line, without opening it.
With these features, it is especially ideal for automation scripts and batch editing of configuration files.
💡 What You Will Learn in This Guide
- structure and syntax of the sed command
- Search and replace operations (
s/eski/yeni/) - In-place editing (
-iand-i.bakoptions) - Perform line deletion (
d), printing (p) and pattern matching operations - Advanced sed usage: regular expressions, range operations, backup
⚙️ 1. sed Command Basics
Basic Syntax
sed [seçenekler] 'komut' dosya
[options]: Determines the behavior of the command (such as -i, -n).
'command': The editing action to apply (s, d, p etc.).
file: The path to the file to be processed.
Example:
sed 's/Linux/Unix/' system.txt
This command replaces the first word "Linux" with "Unix" in each line in the system.txt file.
🧰 2. Most Commonly Used sed Options
| Option | Description |
|---|---|
| -i | In-place editing — saves the file directly |
| -i.bak | Creates backup with extension .bak before editing |
| -n | Prevents automatic output suppression |
| -e | Runs multiple commands in the same call |
| -r or -E | Provides extended regex support |
🔍 3. Search and Replace Operations
Basic Usage (s Command)
sed 's/eski/yeni/' dosya.txt
Replaces the first word "old" in the line with "new".
Replace All Matches in Row
sed 's/Linux/GNU\/Linux/g' metin.txt
The g flag changes matches across the entire line.
Case Insensitive Substitution
sed 's/admin/user/I' config.txt
It makes changes regardless of “Admin”, “ADMIN” or “admin”.
💾 4. In-place Editing
Permanent Edit
sed -i 's/eski_domain/ornek.com/g' vhost.conf
It directly modifies and saves the file.
Editing with Backup
sed -i.yedek 's/test_ortami/canli_sistem/g' env.yaml
Edits the env.yaml file, keeps the old version as env.yaml.backup.
✂️ 5. Line Based Transactions
| Transaction | Command | Description |
|---|---|---|
| Delete specific row | sed '2d' log.txt | 2. deletes row |
| Delete row by pattern | sed '/hata/d' log.txt | deletes lines containing "error" |
| Print line spacing | sed -n '1,3p' log.txt | 1–3. prints lines |
| Delete empty lines | sed '/^$/d' veriler.txt | Removes all blank lines |
🧩 6. Advanced sed Uses
Pattern Matching at the Beginning or End of a Line
sed 's/^root/yonetici/' passwd.txt # Satır başında
sed 's/$/\//' klasorler.txt # Satır sonunda
Replacement on Specific Line Number
sed '4 s/url/link/' config.json
It just changes the first “url” on line 4.
Action by Line Spacing
sed '1,5 s/Linux/Unix/' file.txt
Converts “Linux” → “Unix” in lines 1–5.
Replacing Tab Characters with Spaces
sed 's/\t/ /g' tablo.txt
⚙️ 7. Differences Between sed, grep and awk
| Feature | sed | grep | awk |
|---|---|---|---|
| Purpose | Edit | Search | Data processing |
| On-site editing | ✅ | ❌ | ❌ |
| Regex support | ✅ | ✅ | ✅ |
| Multiprocessing | ✅ | ❌ | ✅ |
| Reporting ability | ❌ | ❌ | ✅ |
🧠 8. Performance Tips
Use -n: Reduces processing time by suppressing unnecessary output.
Combine commands: multitask in one line with -e.
Use pipe (|): Perform stream-based processing instead of creating files.
Don't use -i on large files: Test first, replace later.
Know the alternatives: awk or perl are faster in some cases.
❓ Frequently Asked Questions (FAQ)
1. Why doesn't the sed command modify the file?
By default, sed writes the result to the screen. Permanent change requires the -i parameter.
2. Why use single quotes (‘) in sed?
Prevents misinterpretation of shell characters ($, !, ).
3. What is the difference between sed, grep and awk?
sed edits, grep searches, awk processes and reports.
4. Can I convert tabs to spaces with sed?
Yes:
bash
sed 's/\t/ /g' file.txt
5. Is it possible to remove blank lines with sed?
Yes:
bash
sed '/^$/d' file.txt
🧾 Command Summary
| Quest | Command | Description |
|---|---|---|
| Search and replace | sed 's/eski/yeni/' | Replaces first match |
| Global replacement | sed 's/eski/yeni/g' | Changes all matches |
| On-site editing | sed -i 's/x/y/' | Edits the file directly |
| Delete row | sed '2d' | 2. removes line |
| Erase with pattern | sed '/hata/d' | deletes lines containing “error” |
| Remove blank lines | sed '/^$/d' | Deletes all blank lines |
🌟 Result
The sed command in Linux is not a simple text tool — it is the secret weapon of system administrators, developers, and DevOps engineers. It is indispensable for fast editing, automatic configuration and efficient log analysis of large files.
💡 Experience the power of sed by testing these commands on your GenixNode Servers. Save time, reduce human error, manage the system professionally. 🚀

