GNU sed 4.9 (stable as of 2025-12-30). A stream editor for filtering and transforming text.
Core Syntax
The fundamental operation in sed is substitution. This block demonstrates basic text replacement.
# Example file content:
# line 1: This is a test.
# line 2: Another test line.
# line 3: Test complete.
# Basic substitution: 's/regexp/replacement/flags'
echo "This is a test." | sed 's/test/example/' # Replaces 'test' with 'example' on the first match per line.
# Output: This is a example.
# Global substitution (g flag): replaces all occurrences on a line.
echo "test test test" | sed 's/test/ok/g' # Replaces all 'test' with 'ok'.
# Output: ok ok ok
# Case-insensitive substitution (I flag - GNU sed extension).
echo "This Is A Test." | sed 's/test/success/I' # Replaces 'Test' with 'success', ignoring case.
# Output: This Is A success.
Essential Patterns
sed excels at filtering and deleting lines based on patterns or line numbers.
# Example file content (imagine this is 'data.txt'):
# Line 1: Header
# Line 2: Item A: Value 1
# Line 3: Item B: Value 2
# Line 4: Item C: Value 3
# Line 5: Footer
# Deleting specific lines by number.
sed '1d' data.txt # Deletes the first line.
sed '3,5d' data.txt # Deletes lines from 3 to 5 (inclusive).
# Deleting lines matching a pattern.
sed '/Footer/d' data.txt # Deletes any line containing "Footer".
# Printing only specific lines (requires -n to suppress default output).
sed -n '2p' data.txt # Prints only the second line.
sed -n '/Item/p' data.txt # Prints only lines containing "Item".
# In-place editing (use with caution, always backup or test first).
# Creates a backup 'data.txt.bak' and modifies 'data.txt'.
# sed -i.bak 's/Value/DATA/' data.txt
# No backup, directly modifies 'data.txt'.
# sed -i 's/Value/DATA/' data.txt
Common Use Cases
Beyond simple replacement, sed can insert, append, and transform text based on context.
# Appending text after a line matching a pattern.
echo "Start" | sed '/Start/a\
> New line after Start.' # Appends a new line after "Start".
# Output:
# Start
# New line after Start.
# Inserting text before a line matching a pattern.
echo "End" | sed '/End/i\
> New line before End.' # Inserts a new line before "End".
# Output:
# New line before End.
# End
# Replacing an entire line matching a pattern.
echo "Old line content" | sed '/Old line/c\
> Replaced line content.' # Replaces the line containing "Old line" entirely.
# Output: Replaced line content.
# Using different delimiters for substitution (useful when '/' is in pattern/replacement).
echo "/path/to/file" | sed 's#/path/to/file#/new/path#' # Uses '#' as delimiter.
# Output: /new/path
# Extracting specific fields using regex groups.
echo "Name: John, Age: 30" | sed -n 's/Name: \([^,]*\), Age: \(.*\)/\1 is \2 years old./p'
# Matches "Name: (group1), Age: (group2)" and prints a formatted string.
# Output: John is 30 years old.
Gotchas & Best Practices
sed can be powerful but also destructive. Always test and understand its behavior.
# Always use -i with a backup extension, or test on copies.
# sed -i.bak 's/error/warning/g' logfile.txt # Creates logfile.txt.bak
# Quoting: Single quotes preserve literal interpretation of special characters.
# Double quotes allow shell variable expansion but require escaping sed's special chars.
MY_VAR="replacement"
echo "text" | sed "s/text/$MY_VAR/" # Works, $MY_VAR is expanded by shell.
echo "text" | sed 's/text/'"$MY_VAR"'/' # Safer for complex patterns, shell expands only MY_VAR.
# Be aware of greedy vs. non-greedy matching in regex (GNU sed doesn't directly support non-greedy).
# To match shortest possible string, avoid '.*' if possible or use character classes.
echo "<a><b><c>" | sed 's/<.*>//' # Greedy: matches from first '<' to last '>'.
# Output: (empty line)
echo "<a><b><c>" | sed 's/<[^>]*>//g' # Matches non-'>' characters, effectively non-greedy.
# Output:
Advanced Techniques
Leverage sed’s hold space for multi-line processing and more complex transformations.
# Hold space: 'h' copies pattern space to hold space, 'g' copies hold space to pattern space.
# 'H' appends pattern space to hold space, 'G' appends hold space to pattern space.
# 'x' exchanges pattern and hold space.
# Example: Process two lines, then print them in reverse order.
# Input:
# Line 1
# Line 2
sed '
N; # Append next line to pattern space (now holds "Line 1\nLine 2")
s/\(.*\)\n\(.*\)/\2\n\1/; # Swap lines using backreferences
' <<EOF
Line 1
Line 2
EOF
# Output:
# Line 2
# Line 1
# Example: Read a line, store it in hold space, then print it after the next line.
# Input:
# A
# B
# C
sed '
1h;1d; # On first line: copy to hold space, delete from pattern space.
x; # Exchange pattern and hold space (pattern space now has 'A', hold space has current line).
G; # Append hold space (current line) to pattern space (now 'A\nB').
' <<EOF
A
B
C
EOF
# Output:
# B
# A
# C
# (Note: C is not processed by the x;G block as it's the last line)
# Multi-command scripts using -e or semicolons.
echo "hello world" | sed -e 's/hello/hi/' -e 's/world/there/'
# Output: hi there
echo "hello world" | sed 's/hello/hi/; s/world/there/'
# Output: hi there
Quick Reference
sed 's/regexp/replacement/flags': Basic substitution.gflag: Global replacement on a line.Iflag (GNU): Case-insensitive match.dcommand: Delete lines.pcommand: Print lines (often with-n).-noption: Suppress automatic printing of pattern space.-i[SUFFIX]option: In-place editing, optionally with backup.a\command: Append text after a line.i\command: Insert text before a line.c\command: Change (replace) an entire line.h,H,g,G,x: Hold space commands for multi-line processing.Ncommand: Append next line of input into the pattern space.Pcommand: Print the first line of the pattern space.b label: Branch tolabel.t label: Branch if a substitution has been successful since the last input line was read.&in replacement: Represents the matchedregexp.\1,\2: Backreferences to captured groups inregexp.- Delimiters: Any character can be used instead of
/(e.g.,s#old#new#).
References
- GNU sed manual
- DigitalOcean - Mastering sed Command in Linux
- Hostinger - Linux sed command: How to use and practical examples
- GeeksforGeeks - Sed Command in Linux/Unix With Examples
This page is AI-assisted. References official documentation.