sed Command Cheat Sheet
sed (Stream Editor) is a powerful text stream editor for filtering and transforming text. It performs basic text manipulation on files and streams without opening the file in an editor. Perfect for automated editing and text processing in scripts.
Synopsis
sed [OPTIONS] 'command' [file...]
sed [OPTIONS] -e 'command1' -e 'command2' [file...]
sed [OPTIONS] -f scriptfile [file...]
Description
sed reads input line by line, applies editing commands to each line, and writes the result to standard output. It's commonly used for search-and-replace operations, selective printing, and in-place file modifications.
Basic Substitution
Replace First Occurrence
sed 's/old/new/' file.txt
Output shows replacement on first match per line.
Replace All Occurrences (Global)
sed 's/old/new/g' file.txt
The g flag makes substitution global on each line.
Replace Nth Occurrence
# Replace second occurrence on each line
sed 's/old/new/2' file.txt
# Replace from second occurrence onward
sed 's/old/new/2g' file.txt
Case-Insensitive Replace
sed 's/old/new/gi' file.txt
sed 's/old/new/I' file.txt # Alternative
In-Place Editing
Modify File Directly
sed -i 's/old/new/g' file.txt
Create Backup Before Editing
# Linux/GNU sed
sed -i.bak 's/old/new/g' file.txt
# macOS/BSD sed
sed -i '.bak' 's/old/new/g' file.txt
Creates file.txt.bak with original content.
Line Addressing
Specific Line Number
# Substitute only on line 3
sed '3s/old/new/' file.txt
# Delete line 5
sed '5d' file.txt
Line Range
# Lines 1 to 5
sed '1,5s/old/new/g' file.txt
# From line 10 to end
sed '10,$s/old/new/g' file.txt
Pattern Matching
# Lines containing "pattern"
sed '/pattern/s/old/new/g' file.txt
# Lines NOT containing "pattern"
sed '/pattern/!s/old/new/g' file.txt
Line Range Between Patterns
# From first occurrence of start to first occurrence of end
sed '/start/,/end/s/old/new/g' file.txt
Deletion Commands
Delete Specific Lines
# Delete line 3
sed '3d' file.txt
# Delete lines 2-5
sed '2,5d' file.txt
# Delete last line
sed '$d' file.txt
Delete Lines Matching Pattern
# Delete lines containing "pattern"
sed '/pattern/d' file.txt
# Delete empty lines
sed '/^$/d' file.txt
# Delete lines starting with #
sed '/^#/d' file.txt
# Delete lines containing only whitespace
sed '/^[[:space:]]*$/d' file.txt
Print Commands
Print Specific Lines
# Print line 5 only
sed -n '5p' file.txt
# Print lines 10-20
sed -n '10,20p' file.txt
# Print last line
sed -n '$p' file.txt
Print Lines Matching Pattern
# Print lines containing "error"
sed -n '/error/p' file.txt
# Like grep, but with sed
sed -n '/ERROR/p' log file.txt
Print with Line Numbers
sed -n '=' file.txt # Line numbers only
sed = file.txt | sed 'N;s/\n/ /' # Numbers with content
Insert and Append
Insert Line Before
# Insert before line 3
sed '3i\New line text' file.txt
# Insert before lines matching pattern
sed '/pattern/i\New line' file.txt
Append Line After
# Append after line 3
sed '3a\New line text' file.txt
# Append after matching pattern
sed '/pattern/a\New line' file.txt
Multiple Lines
# Insert multiple lines
sed '3i\Line 1\nLine 2\nLine 3' file.txt
# Append multiple lines
sed '5a\First line\nSecond line' file.txt
Change (Replace) Lines
Replace Entire Line
# Replace line 3
sed '3c\Completely new line' file.txt
# Replace lines matching pattern
sed '/pattern/c\Replacement line' file.txt
# Replace line range
sed '5,10c\One line replaces all' file.txt
Multiple Commands
Using -e Flag
sed -e 's/foo/bar/' -e 's/hello/goodbye/' file.txt
Using Semicolon
sed 's/foo/bar/; s/hello/goodbye/' file.txt
Using -f Script File
Create script.sed:
s/old1/new1/g
s/old2/new2/g
/pattern/d
Run:
sed -f script.sed file.txt
Advanced Substitution
Using Different Delimiters
# Useful for paths
sed 's|/old/path|/new/path|g' file.txt
sed 's#http://old#https://new#g' file.txt
Capture Groups and Backreferences
# Swap two words
sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/' file.txt
# Extract part of pattern
echo "Email: user@example.com" | sed 's/.*:\s*\(.*\)/\1/'
# Output: user@example.com
Using & for Matched String
# Add quotes around matched word
sed 's/[a-zA-Z]*/\"&\"/' file.txt
# Add brackets around numbers
echo "Error 404" | sed 's/[0-9]*/[&]/'
# Output: Error [404]
Regular Expressions
Anchors
# Lines starting with "Error"
sed -n '/^Error/p' file.txt
# Lines ending with "done"
sed -n '/done$/p' file.txt
Character Classes
# Replace any digit
sed 's/[0-9]/X/g' file.txt
# Replace whitespace
sed 's/[[:space:]]/_/g' file.txt
# Remove non-alphanumeric
sed 's/[^a-zA-Z0-9]//g' file.txt
Quantifiers
# One or more digits
sed 's/[0-9]\+/NUM/g' file.txt
# Zero or more spaces
sed 's/[[:space:]]*$//' file.txt # Trim trailing spaces
# Exactly 3 letters
sed 's/[a-z]\{3\}/XXX/g' file.txt
Practical Examples
Remove Comments
# Remove lines starting with #
sed '/^#/d' config.conf
# Remove inline comments
sed 's/#.*//' file.txt
# Remove both
sed -e '/^#/d' -e 's/#.*//' config.conf
Remove Blank Lines
# Remove empty lines
sed '/^$/d' file.txt
# Remove lines with only whitespace
sed '/^[[:space:]]*$/d' file.txt
# Squeeze multiple blank lines to one
sed '/^$/N;/^\n$/D' file.txt
Add Line Numbers
sed = file.txt | sed 'N;s/\n/\t/'
Convert DOS to Unix Line Endings
sed 's/\r$//' dosfile.txt > unixfile.txt
sed -i 's/\r$//' file.txt # In-place
Convert Unix to DOS Line Endings
sed 's/$/\r/' unixfile.txt > dosfile.txt
Extract Email Addresses
sed -n 's/.*\([a-zA-Z0-9._%+-]\+@[a-zA-Z0-9.-]\+\.[a-zA-Z]\{2,\}\).*/\1/p' file.txt
Extract IP Addresses
sed -n 's/.*\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p' log.txt
Replace Tabs with Spaces
sed 's/\t/ /g' file.txt # 4 spaces
Join Lines
# Join lines ending with backslash
sed ':a;N;$!ba;s/\\\n//g' file.txt
# Join all lines
sed ':a;N;$!ba;s/\n/ /g' file.txt
Double Space File
sed 'G' file.txt
Remove Trailing Whitespace
sed 's/[[:space:]]*$//' file.txt
Number Non-Blank Lines
sed '/./=' file.txt | sed '/./N; s/\n/ /'
Working with Files
Edit Multiple Files
sed -i.bak 's/old/new/g' *.txt
Process from Stdin
echo "Hello World" | sed 's/World/Universe/'
cat file.txt | sed 's/foo/bar/'
Output to Different File
sed 's/old/new/g' input.txt > output.txt
Advanced Features
Hold Space and Pattern Space
# Reverse file (like tac)
sed '1!G;h;$!d' file.txt
# Print every other line
sed 'n;d' file.txt # Odd lines
sed '1d;n;d' file.txt # Even lines
Labels and Branching
# Remove duplicate consecutive lines
sed '$!N; /^\(.*\)\n\1$/!P; D'
Conditional Execution
# Replace only if line contains "pattern"
sed '/pattern/{s/old/new/g;}' file.txt
Common Use Cases
Configuration Files
# Update configuration value
sed -i 's/^DEBUG=.*/DEBUG=True/' config.ini
# Add setting if not exists
sed -i '/^API_KEY=/!s/$/\nAPI_KEY=abc123/' .env
Log Processing
# Extract errors from log
sed -n '/ERROR/p' application.log
# Show last 100 lines with timestamp
tail -n 100 app.log | sed 's/^/['"$(date)"'] /'
HTML/XML Processing
# Remove HTML tags
sed 's/<[^>]*>//g' page.html
# Extract title
sed -n 's/.*<title>\(.*\)<\/title>.*/\1/p' page.html
CSV Processing
# Change delimiter from comma to pipe
sed 's/,/|/g' data.csv
# Remove quotes
sed 's/"//g' data.csv
# Extract specific column (2nd)
sed 's/[^,]*,\([^,]*\),.*/\1/' data.csv
Source Code
# Add copyright header
sed '1i\// Copyright 2024\n// All rights reserved\n' source.c
# Remove debug print statements
sed -i '/console\.log/d' script.js
# Comment out code lines
sed 's/^\(.*\)/# \1/' script.py
Tips and Best Practices
- Test without -i first - View output before modifying files
- Use backups - Always use
-i.bakwhen editing important files - Quote commands - Single quotes prevent shell interpretation
- Use different delimiters - Helpful when working with paths (
s|/path|/new|) - Combine with other tools - Pipe with
grep,awk,cutfor powerful text processing - Extended regex - Use
-Eor-rfor extended regular expressions - Script files - Use
-ffor complex multi-command operations
Options Summary
| Option | Description |
|---|---|
-n |
Suppress automatic printing (use with p) |
-e |
Add script command |
-f |
Read script from file |
-i[SUFFIX] |
Edit files in-place (with optional backup) |
-r, -E |
Use extended regular expressions |
-s |
Treat files as separate (don't concatenate) |
-u |
Unbuffered output |
--posix |
Disable GNU extensions |
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Invalid command, syntax error, or invalid regex |
| 2 | Input file could not be opened |
| 4 | I/O error |