sed Command Guide
sed (stream editor) is a powerful tool for text transformation. Learn how to search, replace, and manipulate text from the command line.
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Substitute
s/old/new/Replace first matchs/old/new/gReplace all matchess/old/new/iCase-insensitiveIn-place
-i ''Edit file in place (macOS)-i.bakIn-place with backup-iEdit file in place (Linux)Delete
/pattern/dDelete matching lines1dDelete first line$dDelete last line-n /pattern/pPrint matching lines1,5pPrint lines 1-5/start/,/end/pPrint rangeCommon
s/^/# /Add prefix to liness/$/;/Add suffix to lines/^$/dDelete empty linesDownloadable Image Preview
Basic Syntax
sed processes text line by line, applying commands to each line.
sed 'command' filenameYou can also pipe input to sed:
echo "hello world" | sed 's/world/universe/'Substitution (s command)
The most common sed operation is substitution.
Basic substitution
# Replace first occurrence on each line
sed 's/old/new/' file.txtGlobal substitution (g flag)
# Replace all occurrences on each line
sed 's/old/new/g' file.txtCase-insensitive (i flag)
sed 's/error/warning/gi' file.txtReplace nth occurrence
# Replace only the 2nd occurrence
sed 's/the/THE/2' file.txtSubstitution Flags
| g | Global - replace all occurrences |
| i | Case-insensitive matching |
| p | Print the modified line |
| w file | Write result to file |
| n | Replace nth occurrence |
In-place Editing
Use -i to edit files directly.
# Edit file in place (GNU sed)
sed -i 's/old/new/g' file.txt
# Create backup before editing
sed -i.bak 's/old/new/g' file.txt
# macOS sed requires empty string for no backup
sed -i '' 's/old/new/g' file.txt-i first, or create a backup with -i.bak.Deleting Lines
# Delete lines containing pattern
sed '/pattern/d' file.txt
# Delete specific line number
sed '5d' file.txt
# Delete range of lines
sed '5,10d' file.txt
# Delete from line 5 to end
sed '5,$d' file.txt
# Delete empty lines
sed '/^$/d' file.txtPrinting Lines
# Print only matching lines (-n suppresses default output)
sed -n '/pattern/p' file.txt
# Print specific line
sed -n '5p' file.txt
# Print range of lines
sed -n '5,10p' file.txt
# Print lines between two patterns
sed -n '/start/,/end/p' file.txtAddress Ranges
You can apply commands to specific lines or ranges.
# Apply to line 3
sed '3s/old/new/' file.txt
# Apply to lines 3-7
sed '3,7s/old/new/' file.txt
# Apply from line 5 to end
sed '5,$s/old/new/' file.txt
# Apply to lines matching pattern
sed '/error/s/old/new/' file.txt
# Apply to lines NOT matching pattern
sed '/pattern/!s/old/new/' file.txtUsing Regular Expressions
# Match word boundaries
sed 's/\bword\b/replacement/g' file.txt
# Use extended regex (-E or -r)
sed -E 's/[0-9]+/NUMBER/g' file.txt
# Capture groups
sed -E 's/([0-9]+)-([0-9]+)/\2-\1/g' file.txt
# Match beginning/end of line
sed 's/^/PREFIX: /' file.txt
sed 's/$/ :SUFFIX/' file.txt-E (or -r on some systems) for extended regular expressions with features like +, ?, and unescaped parentheses.Practical Examples
Remove HTML tags
sed 's/<[^>]*>//g' file.htmlConvert DOS to Unix line endings
sed 's/\r$//' file.txtAdd line numbers
sed = file.txt | sed 'N;s/\n/\t/'Extract email addresses
sed -n 's/.*\([a-zA-Z0-9._-]*@[a-zA-Z0-9._-]*\).*/\1/p' file.txtComment out lines containing pattern
sed '/DEBUG/s/^/# /' config.txtReplace in multiple files
sed -i 's/old/new/g' *.txtMultiple Commands
Chain multiple sed commands together.
# Using -e flag
sed -e 's/foo/bar/' -e 's/baz/qux/' file.txt
# Using semicolons
sed 's/foo/bar/; s/baz/qux/' file.txt
# Using newlines (in a script)
sed '
s/foo/bar/
s/baz/qux/
/delete/d
' file.txtSummary
sed is a powerful text processing tool. Key takeaways:
- Use
s/old/new/gfor global substitution - Use
-ifor in-place editing (with caution) - Use
-nwithpto print specific lines - Use address ranges to target specific lines
- Use
-Efor extended regular expressions
Official Documentation
For authoritative information, refer to the official documentation:
Related Articles
awk Command Guide
Text processing and data extraction
grep Command: Practical Examples & Usage Guide
Master grep with practical examples for developers. Learn pattern matching, regex, and real-world use cases.
xargs Command Guide
Build commands from standard input