Terminal GuideTerminal Guide

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.

8 min readLast updated: January 19, 2025
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Substitute

s/old/new/Replace first match
s/old/new/gReplace all matches
s/old/new/iCase-insensitive

In-place

-i ''Edit file in place (macOS)
-i.bakIn-place with backup
-iEdit file in place (Linux)

Delete

/pattern/dDelete matching lines
1dDelete first line
$dDelete last line

Print

-n /pattern/pPrint matching lines
1,5pPrint lines 1-5
/start/,/end/pPrint range

Common

s/^/# /Add prefix to lines
s/$/;/Add suffix to lines
/^$/dDelete empty lines

Downloadable Image Preview

Failed to generate preview

Basic Syntax

sed processes text line by line, applying commands to each line.

bash
sed 'command' filename

You can also pipe input to sed:

bash
echo "hello world" | sed 's/world/universe/'

Substitution (s command)

The most common sed operation is substitution.

Basic substitution

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

Global substitution (g flag)

bash
# Replace all occurrences on each line
sed 's/old/new/g' file.txt

Case-insensitive (i flag)

bash
sed 's/error/warning/gi' file.txt

Replace nth occurrence

bash
# Replace only the 2nd occurrence
sed 's/the/THE/2' file.txt

Substitution Flags

gGlobal - replace all occurrences
iCase-insensitive matching
pPrint the modified line
w fileWrite result to file
nReplace nth occurrence

In-place Editing

Use -i to edit files directly.

bash
# 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
Warning
Always test your sed command without -i first, or create a backup with -i.bak.

Deleting Lines

bash
# 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.txt

Printing Lines

bash
# 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.txt

Address Ranges

You can apply commands to specific lines or ranges.

bash
# 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.txt

Using Regular Expressions

bash
# 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
Tip
Use -E (or -r on some systems) for extended regular expressions with features like +, ?, and unescaped parentheses.

Practical Examples

Remove HTML tags

bash
sed 's/<[^>]*>//g' file.html

Convert DOS to Unix line endings

bash
sed 's/\r$//' file.txt

Add line numbers

bash
sed = file.txt | sed 'N;s/\n/\t/'

Extract email addresses

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

Comment out lines containing pattern

bash
sed '/DEBUG/s/^/# /' config.txt

Replace in multiple files

bash
sed -i 's/old/new/g' *.txt

Multiple Commands

Chain multiple sed commands together.

bash
# 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.txt

Summary

sed is a powerful text processing tool. Key takeaways:

  • Use s/old/new/g for global substitution
  • Use -i for in-place editing (with caution)
  • Use -n with p to print specific lines
  • Use address ranges to target specific lines
  • Use -E for extended regular expressions

Official Documentation

For authoritative information, refer to the official documentation:

Related Articles