Skip to content
sed Command in Linux — Stream Editor with Practical Examples

sed Command in Linux — Stream Editor with Practical Examples

DodaTech Updated Jun 20, 2026 8 min read

sed (stream editor) is a Linux command that reads text line by line, applies editing operations, and writes the result to standard output. Unlike interactive editors like vim or nano, sed is non-interactive — perfect for scripting, batch processing, and automation.

What You’ll Learn

By the end of this tutorial, you’ll know how to use sed for text substitution with global and per-line flags, delete lines by number or pattern, insert and append text, edit files in-place, chain multiple commands, use address ranges, and capture groups with regex backreferences.

Why sed Matters

sed is one of the fastest ways to transform text files without opening them. Configuration file changes, log sanitization, data cleaning, and mass find-and-replace across hundreds of files — these are all sed tasks. DodaZIP uses sed in its build pipeline to update version strings across configuration files, and Durga Antivirus Pro uses it to sanitize log data before analysis.

sed Learning Path

    flowchart LR
  A[grep Command] --> B[sed Command<br/>You are here]
  B --> C[awk Command]
  C --> D[xargs Command]
  D --> E[Shell Scripting]
  style B fill:#f90,color:#fff
  
Prerequisites: Basic Linux command-line skills. Review essential Linux commands and the grep command tutorial if needed.

Syntax Overview

sed [options] 'command' file...
sed [options] -f script_file file...
OptionDescription
-nSuppress automatic printing (used with p flag)
-iEdit file in-place (optional backup: -i.bak)
-eChain multiple commands
-fRead commands from a script file
-EExtended regex (capture groups without escaping ())

10 Practical Examples

Create a sample file to follow along:

cat > data.txt << 'EOF'
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080
EOF

1. Basic Substitution

Replace first occurrence of “fox” with “cat” on each line:

sed 's/fox/cat/' data.txt
Line 1: The quick brown cat
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown cat
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080

2. Global Flag — Replace All Occurrences

Replace ALL occurrences of “o” with “0” on each line:

sed 's/o/0/g' data.txt
Line 1: The quick br0wn f0x
Line 2: jumps 0ver the lazy d0g
Line 3: THE QUICK BR0WN F0X
Line 4: jumps 0ver the lazy d0g again
Line 5: Repeat the quick br0wn f0x
Line 6:    Indented line here
Line 7:
Line 8: # This is a c0mment
Line 9: server=l0calh0st
Line 10: p0rt=8080

3. Delete Lines by Number

Delete line 3:

sed '3d' data.txt
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080

Delete lines 3 through 5:

sed '3,5d' data.txt
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080

4. Delete Lines by Pattern

Delete all lines starting with #:

sed '/^#/d' data.txt
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6:    Indented line here
Line 7:
Line 9: server=localhost
Line 10: port=8080

Delete empty lines:

sed '/^$/d' data.txt
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6:    Indented line here
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080

5. Insert and Append Lines

Insert a line BEFORE line 5:

sed '5i\Line inserted before line 5' data.txt
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line inserted before line 5
Line 5: Repeat the quick brown fox
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080

Append a line AFTER line 10:

sed '10a\Line appended after line 10' data.txt
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080
Line appended after line 10

6. In-Place Editing

Edit the file directly (creates backup data.txt.bak):

sed -i.bak 's/localhost/127.0.0.1/' data.txt
cat data.txt
Line 1: The quick brown fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=127.0.0.1
Line 10: port=8080

Restore the backup:

mv data.txt.bak data.txt

7. Multiple Commands with -e

Replace “fox” with “cat” AND delete comment lines in one pass:

sed -e 's/fox/cat/' -e '/^#/d' data.txt
Line 1: The quick brown cat
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown cat
Line 6:    Indented line here
Line 7:
Line 9: server=localhost
Line 10: port=8080

8. Address Ranges

Print only lines 2 through 4:

sed -n '2,4p' data.txt
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again

Substitute only on lines 1 through 3:

sed '1,3s/fox/cat/' data.txt
Line 1: The quick brown cat
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the quick brown fox
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080

9. Regex Capture Groups

Swap word order — capture “quick brown” and reinsert reversed:

sed -E 's/(quick) (brown)/\2 \1/' data.txt
Line 1: The brown quick fox
Line 2: jumps over the lazy dog
Line 3: THE QUICK BROWN FOX
Line 4: jumps over the lazy dog again
Line 5: Repeat the brown quick fox
Line 6:    Indented line here
Line 7:
Line 8: # This is a comment
Line 9: server=localhost
Line 10: port=8080

Note: Line 3 is uppercase so it didn’t match.

10. Print Matching Lines Only

Print lines containing “ERROR” from a log:

cat > app.log << 'EOF'
INFO: Server started
ERROR: Connection refused
WARN: Disk full
ERROR: Timeout
EOF

sed -n '/ERROR/p' app.log
ERROR: Connection refused
ERROR: Timeout

Common Use Cases

Remove Trailing Whitespace

sed -i 's/[[:space:]]*$//' file.txt

Convert Line Endings (DOS to Unix)

sed -i 's/\r$//' file.txt

Extract Lines Between Two Markers

sed -n '/START/,/END/p' file.txt

Comment Out Lines Matching a Pattern

sed -i '/pattern/s/^/# /' config.cfg

Common Mistakes

1. Forgetting the -i Flag Without a Backup

sed -i 's/foo/bar/' file.txt modifies the file directly. Always use -i.bak the first time to create a backup.

2. Confusing s/// with Address Ranges

s/old/new/ is the substitute command. 3,5d uses an address range (lines 3-5) with the delete command. They are different operations.

3. Not Escaping Forward Slashes in Patterns

If your pattern contains /, use a different delimiter: s#/usr/bin#/usr/local/bin#g.

4. Using -n Without p

sed -n suppresses output. Without the p flag, nothing prints. Always use sed -n '/pattern/p' to see matches.

Practice Questions

1. How would you replace all occurrences of “localhost” with “192.168.1.100” in a config file?

sed -i 's/localhost/192.168.1.100/g' config.cfg

2. What does sed -n '10,20p' file.txt do?

It prints lines 10 through 20 of file.txt.

3. How do you delete all blank lines from a file?

sed '/^$/d' file.txt

4. What’s the difference between sed 's/foo/bar/' and sed 's/foo/bar/g'?

The first replaces only the first occurrence of “foo” per line. The global flag g replaces every occurrence on each line.

5. Challenge: Write a sed command that uppercases the first letter of every word on each line.

sed 's/\b\([a-z]\)/\u\1/g' file.txt (GNU sed supports \u for uppercase in replacement)

Mini Project: Config File Updater

Create a script that safely updates server configuration values:

#!/bin/bash
# update_config.sh — Safely update config values
# Usage: ./update_config.sh config.cfg

CONFIG="$1"
BACKUP="${CONFIG}.bak"

if [ ! -f "$CONFIG" ]; then
    echo "Error: Config file not found: $CONFIG"
    exit 1
fi

cp "$CONFIG" "$BACKUP"
echo "Backup created: $BACKUP"

# Common config updates
sed -i 's/^server=.*/server=192.168.1.100/' "$CONFIG"
sed -i 's/^port=.*/port=9090/' "$CONFIG"
sed -i 's/^debug=.*/debug=false/' "$CONFIG"
sed -i '/^#/d' "$CONFIG"
sed -i '/^$/d' "$CONFIG"

echo "Config updated. Changes:"
diff "$BACKUP" "$CONFIG"

Expected output:

Backup created: config.cfg.bak
Config updated. Changes:
9c9
< server=localhost
---
> server=192.168.1.100
10c10
< port=8080
---
> port=9090

FAQ

What does the -n flag do in sed?
-n suppresses automatic printing of lines. sed normally prints every line after processing. With -n, only lines explicitly printed with the p flag are shown.
Can I use variables in sed commands?
Yes, but use double quotes: sed "s/$old/$new/g" file.txt. Single quotes prevent variable expansion.
How do I match a tab character in sed?
Use sed 's/\t/ /g' in GNU sed, or type a literal tab by pressing Ctrl+V then Tab.
What’s the difference between sed and awk?
sed is primarily a stream editor (find/replace, insert, delete). awk is a full text processing language with fields, variables, and control flow. Use sed for simple substitutions; use awk for column-based or conditional processing.

What’s Next

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro