sed

Stream editor for filtering and transforming text using pattern matching and substitution.

cli
sedtext-processingunixlinuxregex

Basic Substitution

# Replace first occurrence on each line
sed 's/old/new/' file.txt

# Replace all occurrences (global flag)
sed 's/old/new/g' file.txt

# Replace only on specific line
sed '3s/old/new/' file.txt

# Replace in line range
sed '1,5s/old/new/g' file.txt

In-Place Editing

# Edit file in place (GNU sed)
sed -i 's/old/new/g' file.txt

# Edit with backup (creates file.txt.bak)
sed -i.bak 's/old/new/g' file.txt

# macOS/BSD sed requires empty string for no backup
sed -i '' 's/old/new/g' file.txt

Deletion

# Delete specific line
sed '3d' file.txt

# Delete line range
sed '1,5d' file.txt

# Delete last line
sed '$d' file.txt

# Delete lines matching pattern
sed '/pattern/d' file.txt

# Delete empty lines
sed '/^$/d' file.txt

# Delete lines starting with #
sed '/^#/d' file.txt

Insertion and Appending

# Insert text before line 3
sed '3i\New line text' file.txt

# Append text after line 3
sed '3a\New line text' file.txt

# Insert before pattern match
sed '/pattern/i\New line' file.txt

# Append after pattern match
sed '/pattern/a\New line' file.txt

Multiple Commands

# Chain multiple substitutions with -e
sed -e 's/old1/new1/g' -e 's/old2/new2/g' file.txt

# Use semicolon separator
sed 's/old1/new1/g; s/old2/new2/g' file.txt

# Multiple commands from file
sed -f commands.sed file.txt

Regular Expressions

# Match any character (.)
sed 's/c.t/dog/g' file.txt          # Matches cat, cot, cut

# Match start of line (^)
sed 's/^Hello/Hi/g' file.txt

# Match end of line ($)
sed 's/\.$/!/g' file.txt             # Replace period at end with !

# Match digit (\d or [0-9])
sed 's/[0-9]\+/NUM/g' file.txt       # Replace all numbers

# Match word boundaries (\b)
sed 's/\bcat\b/dog/g' file.txt       # Only whole word "cat"

Capturing Groups

# Capture and reuse with \1, \2, etc.
sed 's/\(.*\)@\(.*\)/User: \1, Domain: \2/' emails.txt

# Swap two words
sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/' file.txt

# Extract filename from path
sed 's|.*/\([^/]*\)|\1|' paths.txt

# Wrap matched text
sed 's/\(error\)/**\1**/g' log.txt   # Bold errors in markdown

Line Ranges and Patterns

# Process lines between two patterns
sed '/START/,/END/s/old/new/g' file.txt

# Process from line N to end
sed '10,$s/old/new/g' file.txt

# Process every Nth line (every 2nd line)
sed 'n;s/old/new/g' file.txt

# Print only matching lines
sed -n '/pattern/p' file.txt

Case Conversion

# Convert to uppercase (GNU sed)
sed 's/.*/\U&/' file.txt

# Convert to lowercase (GNU sed)
sed 's/.*/\L&/' file.txt

# Capitalize first letter
sed 's/\b\(.\)/\u\1/g' file.txt

# Using tr for portable case conversion
sed 's/.*/\L&/' file.txt | tr '[:lower:]' '[:upper:]'

Practical Examples

# Remove trailing whitespace
sed 's/[[:space:]]*$//' file.txt

# Remove leading whitespace
sed 's/^[[:space:]]*//' file.txt

# Remove HTML tags
sed 's/<[^>]*>//g' file.html

# Extract URLs from HTML
sed -n 's/.*href="\([^"]*\)".*/\1/p' page.html

# Comment out lines
sed 's/^/# /' script.sh

# Uncomment lines
sed 's/^# //' script.sh

# Add line numbers
sed = file.txt | sed 'N;s/\n/\t/'

# Replace tabs with spaces
sed 's/\t/    /g' file.txt

Delimiters

# Use different delimiter for paths (avoids escaping /)
sed 's|/old/path|/new/path|g' file.txt
sed 's#/old/path#/new/path#g' file.txt
sed 's@/old/path@/new/path@g' file.txt

# Useful when working with URLs
sed 's|http://|https://|g' urls.txt

Printing and Suppressing

# Print only modified lines
sed -n 's/old/new/p' file.txt

# Suppress automatic printing (-n flag)
sed -n '1,5p' file.txt               # Print lines 1-5 only

# Print line numbers with content
sed -n '=' file.txt

# Print lines containing pattern
sed -n '/pattern/p' file.txt

Advanced Patterns

# Remove comments and empty lines
sed '/^#/d; /^$/d' config.txt

# Extract email addresses
sed -n 's/.*\([a-zA-Z0-9._-]*@[a-zA-Z0-9._-]*\).*/\1/p' file.txt

# Format CSV to TSV
sed 's/,/\t/g' data.csv

# Convert DOS line endings to Unix
sed 's/\r$//' dos-file.txt

# Double-space a file
sed 'G' file.txt

# Remove duplicate blank lines
sed '/^$/{ N; /^\n$/D; }'

Backup and Safety

# Always test without -i first
sed 's/old/new/g' file.txt > output.txt

# Preview changes before committing
sed 's/old/new/g' file.txt | diff file.txt -

# Process multiple files safely
for file in *.txt; do
  sed 's/old/new/g' "$file" > "${file}.tmp" && mv "${file}.tmp" "$file"
done