How to Use Sed to Replace
• 2 min read
bash sed stream editor replace substitution text processing
Quick Answer: Replace Text with Sed
To replace text, use sed 's/old/new/' file.txt for the first occurrence per line or sed 's/old/new/g' file.txt for all occurrences. For in-place editing, use sed -i 's/old/new/g' file.txt. The ‘s’ command is sed’s substitution command.
Quick Comparison: Sed Replacement Methods
| Syntax | What It Does | Best For | Notes |
|---|---|---|---|
| s/old/new/ | First on line | Basic replace | Simplest |
| s/old/new/g | All on line | Global replace | Most common |
| s/old/new/gi | All, case-insensitive | Ignoring case | Useful for mixed case |
| -i flag | In-place edit | Modifying files | Creates backup with -i.bak |
Bottom line: Use s/old/new/g for global replacement; add i for case-insensitive.
Use sed (stream editor) to find and replace text in files and streams. Learn basic substitution, advanced patterns, and file editing.
Method 1: Basic Substitution
# Replace first occurrence per line
sed 's/old/new/' file.txt
# Replace all occurrences per line
sed 's/old/new/g' file.txt
# Show on stdout (doesn't modify file)
Detailed Example
Test file (data.txt):
apple is red
banana is yellow
apple is sweet
# Replace first "apple" on each line
sed 's/apple/orange/' data.txt
# Output:
# orange is red
# banana is yellow
# orange is sweet
# Replace all occurrences
sed 's/apple/orange/g' data.txt
# Output:
# orange is red
# banana is yellow
# orange is sweet
In-Place File Editing
# Modify file directly (with backup)
sed -i.bak 's/old/new/g' file.txt
# Modify without backup
sed -i 's/old/new/g' file.txt
Case-Insensitive Replacement
# Case-insensitive flag (i)
sed 's/apple/orange/gi' file.txt
Replace on Specific Lines
# Replace only on line 2
sed '2s/old/new/' file.txt
# Replace on lines 2-4
sed '2,4s/old/new/' file.txt
# Replace on line 5 onwards
sed '5,$s/old/new/' file.txt
Replace with Pattern Matching
# Only replace on lines containing "pattern"
sed '/pattern/s/old/new/' file.txt
# Replace on lines matching regex
sed '/^Error:/s/failed/error/' logfile.txt
# Replace if line contains specific text
sed '/^[0-9]/s/^/#/' file.txt # Comment out lines starting with digit
Use Different Delimiter
# Use | instead of / (useful for paths)
sed 's|/old/path|/new/path|' file.txt
# Use # as delimiter
sed 's#old#new#' file.txt
Practical Example: Configuration Update
#!/bin/bash
# File: update_config.sh
config_file="$1"
# Backup original
cp "$config_file" "$config_file.backup"
# Update multiple settings
sed -i 's/^debug=false/debug=true/' "$config_file"
sed -i 's/^port=8080/port=9000/' "$config_file"
sed -i 's/^host=localhost/host=0.0.0.0/' "$config_file"
echo "✓ Configuration updated"
echo "Backup saved: $config_file.backup"
Usage:
$ chmod +x update_config.sh
$ ./update_config.sh app.conf
Escape Special Characters
# For replacements with special regex characters, escape them
sed 's/\./\\./g' file.txt # Escape dots
sed 's/\$/USD/g' file.txt # Escape dollar sign
sed 's/\*/asterisk/g' file.txt # Escape asterisk
Address Ranges with Substitution
# Replace between two patterns
sed '/START/,/END/s/old/new/' file.txt
# Replace on odd-numbered lines
sed -n '1~2p' file.txt # Show odd lines
sed '1~2s/old/new/' file.txt # Replace on odd lines
# Replace on even-numbered lines
sed '0~2s/old/new/' file.txt
Multiple Substitutions
# Using -e for multiple patterns
sed -e 's/apple/orange/' -e 's/banana/grape/' file.txt
# Or in script file
cat > replacements.sed << 'EOF'
s/apple/orange/
s/banana/grape/
s/cherry/lemon/
EOF
sed -f replacements.sed file.txt
Advanced Substitution with Captures
# Capture groups and backreferences
sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/' file.txt
# Swap words: "hello world" becomes "world hello"
echo "hello world" | sed 's/\([^ ]*\) \([^ ]*\)/\2 \1/'
Print Modified Lines
# Show only lines that were changed
sed -n 's/old/new/p' file.txt
# Show all lines except those matching pattern
sed '/pattern/d' file.txt
Practical Example: Log File Processing
#!/bin/bash
# File: process_log.sh
logfile="$1"
output="${2:-processed.log}"
# Clean up log: remove timestamps, IPs, and sensitive data
sed \
-e 's/\[.*\]/[LOG]/g' \
-e 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/IP_REDACTED/g' \
-e 's/user[0-9]*/USER_X/g' \
"$logfile" > "$output"
echo "Processed log saved to: $output"
Global vs Line-Specific
# Replace only first occurrence on line
sed 's/pattern/replacement/' file.txt
# Replace all occurrences on line
sed 's/pattern/replacement/g' file.txt
# Replace only second occurrence on line
sed 's/pattern/replacement/2' file.txt
# Replace from 2nd occurrence onwards
sed 's/pattern/replacement/2g' file.txt
Conditional Replacement
# Replace only if line contains another pattern
sed '/contains_this/ s/old/new/' file.txt
# Replace only if line does NOT contain pattern
sed '/skip_this/! s/old/new/' file.txt
Backup with Automatic Naming
# Create backup with timestamp
sed -i.$(date +%Y%m%d_%H%M%S) 's/old/new/g' file.txt
# Create backup with number
sed -i~1 's/old/new/g' file.txt # Creates file.txt~1
Using Variables in sed
#!/bin/bash
old_value="$1"
new_value="$2"
file="$3"
# Must use double quotes to expand variables
sed -i "s/$old_value/$new_value/g" "$file"
Usage:
$ ./replace_text.sh "old_text" "new_text" file.txt
Sed with Empty Replacement
# Delete pattern (replace with nothing)
sed 's/pattern//g' file.txt
# Remove leading/trailing spaces
sed 's/^[ \t]*//; s/[ \t]*$//' file.txt
# Remove all whitespace
sed 's/[[:space:]]*//g' file.txt
Common Mistakes
- Forgetting to use -i for in-place - redirects to new file instead
- Not escaping special characters - regex characters need escaping
- Using single quotes with variables - won’t expand
- Confusing line vs global - /g flag makes difference
- Not backing up - use
-i.bakto preserve original
Performance Tips
- Use sed for small to medium files (< 100MB)
- Combine multiple patterns in one sed call
- Use addresses to limit operations to specific lines
- Test with cat first before using -i
Key Points
- Basic syntax:
sed 's/pattern/replacement/g' - Use -i for in-place editing
- Use /g for global (all occurrences)
- Escape special regex characters with \
- Always backup before in-place editing
Summary
Sed is powerful for text replacement. Master the basic substitution syntax, learn to escape special characters, and always test before modifying files in place. Use addresses and conditions for targeted replacements.