// Transform. Replace. Edit.
TEXT IS EVERYTHING.
On Linux, everything is text—config files, logs, code. sed is your scalpel for precise text surgery. Find, replace, insert, delete—on entire files or specific lines.
AUTOMATE EDITS.
Why edit 100 files manually when sed can do it in seconds? Master sed and you'll automate text transformations that would take hours manually.
Click a lesson to begin
What is sed? Basic concepts and usage.
BeginnerThe s command for find and replace.
Beginnerg, i, p flags and more.
BeginnerApply commands to specific lines.
BeginnerUse d to delete lines.
Beginneri, a, c commands for adding text.
Intermediater and w commands for file operations.
IntermediateUse regex patterns in sed.
IntermediateCapture groups with \1 \2.
IntermediateEdit files directly with -i.
AdvancedWork with multiple lines using N.
AdvancedAutomate sed in bash scripts.
Advancedsed (stream editor) reads text from input, transforms it, and outputs the result. It's perfect for batch text operations like find-and-replace.
sed [OPTIONS] 'COMMAND' [FILE...] sed [OPTIONS] -f SCRIPTFILE [FILE...]
# Replace first 'hello' with 'world' in file.txt sed 's/hello/world/' file.txt # Use pipe echo "hello world" | sed 's/hello/goodbye/'
1. What does sed stand for?
The substitution command `s` finds a pattern and replaces it:
s/pattern/replacement/
# Replace 'error' with 'ERROR' sed 's/error/ERROR/' file.log # Replace all occurrences on each line (default is first only) sed 's/error/ERROR/' file.log # Delimiter can be any character sed 's|/bin/bash|/bin/zsh|' /etc/passwd
1. What is the sed substitution command syntax?
g Replace all occurrences on each line (global) i Case insensitive matching p Print the modified line I Case insensitive (GNU extension) E Extended regex (ERE)
# Replace ALL occurrences (not just first) sed 's/error/ERROR/g' file.log # Case insensitive sed 's/error/ERROR/gi' file.log # Print only changed lines (-n suppresses unless p) sed -n 's/error/ERROR/p' file.log # Only print modified lines sed -n 's/root/ADMIN/p' /etc/passwd
1. What flag makes sed replace ALL occurrences on a line?
Addresses specify which lines to operate on. Without an address, sed operates on all lines.
N Line number N $ Last line N,M Lines N through M /pattern/ Lines matching pattern /pat1/,/pat2/ From first match to next match
# Replace on line 5 only sed '5s/old/new/' file.txt # Replace on lines 10-20 sed '10,20s/old/new/' file.txt # Replace on last line sed '$s/old/new/' file.txt # Replace on lines containing 'error' sed '/error/s/old/new/' file.txt # From line matching 'start' to 'end' sed '/^start/,/^end/s/old/new/' file.txt
1. How do you replace on line 10 only?
The delete command removes lines from the output (or file with -i).
# Delete line 5 sed '5d' file.txt # Delete lines 1-10 sed '1,10d' file.txt # Delete last line sed '$d' file.txt # Delete empty lines sed '/^$/d' file.txt # Delete lines containing 'error' sed '/error/d' file.txt # Delete lines NOT containing 'error' (invert) sed '/error/!d' file.txt
1. What command deletes empty lines?
i\ Insert text BEFORE line a\ Append text AFTER line c\ Change (replace) entire line
# Insert before line 3 sed '3i\New line here' file.txt # Append after line 3 sed '3a\New line here' file.txt # Append at end of file sed '$a\Last line added' file.txt # Change (replace) line 5 sed '5c\This replaces the entire line' file.txt # Insert at beginning of file sed '1i\Header line' file.txt
1. Which command inserts text before a line?
r FILE Read file and insert after line w FILE Write lines to file
# Insert contents of header.txt after line 1 sed '1r header.txt' file.txt # Insert footer at end of file sed '$r footer.txt' file.txt # Write all lines containing 'error' to errors.log sed -n '/error/w errors.log' file.txt # Write lines 1-10 to newfile.txt sed -n '1,10w newfile.txt' file.txt
1. What does r FILE do in sed?
sed uses basic regular expressions (BRE) by default. Use -E for extended regex (ERE).
. Any character * Zero or more of preceding ^ Beginning of line $ End of line [] Character class \(\) Group (BRE) \| OR (ERE with -E)
# Replace lines starting with 'error' sed 's/^error/ERROR/' file.log # Replace lines ending with '.log' sed 's/\.log$/LOG_FILE/' file.txt # Replace any digit sed 's/[0-9]/X/g' file.txt # Extended regex (-E) sed -E 's/(error|warning|critical)/[ALERT]/gi' file.log # Remove leading whitespace sed 's/^[[:space:]]*//' file.txt
1. What regex metacharacter matches any character?
Use \( \) to group patterns and \1, \2, etc. to reference them in the replacement.
# Swap first two words echo "hello world" | sed 's/\(hello\) \(world\)/\2 \1/' # Output: world hello # Remove duplicate words sed 's/\([a-z]*\) \1/\1/g' file.txt # Add quotes around words echo "one two three" | sed 's/\([a-z]*\)/"\1"/g' # Output: "one" "two" "three" # Extract and reorder echo "John:Doe:123" | sed 's/\([^:]*\):\([^:]*\):\(.*\)/\2, \1 (\3)/' # Output: Doe, John (123)
1. How do you reference the first capture group?
The -i flag edits files in place (modifies the original file). Use -i.bak to create a backup.
# Edit file.txt in place
sed -i 's/old/new/g' file.txt
# Create backup before editing
sed -i.bak 's/old/new/g' file.txt
# Creates file.txt.bak as backup
# Edit multiple files
sed -i 's/old/new/g' file1.txt file2.txt file3.txt
# Edit only .conf files
for f in *.conf; do sed -i 's/port 80/port 8080/g' "$f"; done
# In-place with regex
sed -i -E 's/[0-9]{4}-[0-9]{2}-[0-9]{2}/YYYY-MM-DD/g' log.txt
Always create backups or test with redirection first!
# Test first (redirect to new file) sed 's/old/new/g' file.txt > newfile.txt # When satisfied, use -i sed -i.bak 's/old/new/g' file.txt
1. What does sed -i.bak do?
N appends the next line to the pattern space, allowing multi-line operations.
# Join lines (replace newlines with space)
sed ':a; N; s/\n/ /g; ba' file.txt
# Delete blank lines
sed '/^$/d' file.txt
# Or
sed -n '/./p' file.txt
# Print lines 1-5 only
sed -n '1,5p' file.txt
# Find lines containing 'error' and the next line
sed -n '/error/{p; n; p}' file.txt
# Convert DOS line endings to Unix sed -i 's/\r$//' file.txt # Remove ^M characters (DOS line endings) sed -i 's/\r//g' file.txt
1. What does the N command do?
#!/bin/bash
# Replace URLs in config file
sed -i.bak \
-e 's|http://old.com|http://new.com|g' \
-e 's|8080|80|g' \
-e 's|/var/data|/var/www|g' \
config.txt
echo "Config updated. Backup: config.txt.bak"
#!/bin/bash
# Extract and format errors from log
sed -n 's/.*\(ERROR\).*/\1/p' app.log | \
sed 's/ERROR/\[ERROR\]/g' | \
sort | uniq -c | sort -rn
# Multiple -e options
sed -e 's/a/A/g' \
-e 's/e/E/g' \
-e 's/i/I/g' \
-e 's/o/O/g' \
-e 's/u/U/g' \
file.txt
# Use semicolons
sed 's/a/A/g; s/e/E/g; s/i/I/g; s/o/O/g; s/u/U/g' file.txt
You've mastered sed! You now understand:
sed is the power tool for text transformation. When you need to make the same change across hundreds of files, sed does it instantly.
Master sed and you'll automate text editing tasks that would take hours manually. Combined with regex, it's incredibly powerful.
Transform. Replace. Automate.