Overview
This series teaches sed through heavily annotated, self-contained shell examples. Each example
focuses on a single concept and includes inline annotations explaining what each command does,
why it matters, and what output or transformation results from it. All examples use echo
piped to sed or heredoc input so you can run them directly in any Unix-like shell without
creating files first.
Series Structure
The examples are organized into three levels based on complexity:
- Beginner — Basic substitution, print and delete commands, line addressing, range addressing, in-place editing, and single-character commands
- Intermediate — Capture groups, extended regex, hold space, multiline processing, branching and labels, and sed script files
- Advanced — GNU vs BSD portability, complex multiline transforms, config file manipulation, log processing, pipeline integration, and real-world automation scripts
Structure of Each Example
Every example follows a consistent five-part format:
- Brief Explanation — what the sed command does and why it matters (2-3 sentences)
- Mermaid Diagram — visual representation of data flow or command execution (when appropriate)
- Heavily Annotated Code — shell commands with
# =>comments describing each flag, address, command, and its effect on the pattern space - Key Takeaway — the core insight to retain from the example (1-2 sentences)
- Why It Matters — production relevance and real-world impact (50-100 words)
How to Use This Series
Each page presents annotated shell one-liners and short scripts. Read the annotations alongside the command to understand both the mechanics and the intent. The examples build on each other within each level, so reading sequentially gives the fullest understanding. Every example is self-contained — you can copy any single example and run it immediately without setup.
Examples by Level
Beginner (Examples 1–28)
- Example 1: Minimal Substitution
- Example 2: Global Flag
g - Example 3: Case-Insensitive Flag
I - Example 4: Printing with
pand-n - Example 5: Deleting Lines with
d - Example 6: Addressing by Line Number
- Example 7: The
$Last-Line Address - Example 8: Range Addressing with Line Numbers
- Example 9: Regex Addressing
- Example 10: Regex Range Addressing
- Example 11: First Occurrence Only (No
gFlag) - Example 12: Nth Occurrence Flag
- Example 13: In-Place Editing with
-i - Example 14: In-Place with Backup (
-i.bak) - Example 15: Multiple Expressions with
-e - Example 16: Commands from a Script File with
-f - Example 17: Append with
a - Example 18: Insert with
i - Example 19: Change with
c - Example 20: Transliterate with
y - Example 21: Quit with
q - Example 22: Read from File with
r - Example 23: Write to File with
w - Example 24: Suppress Output and Print Selectively
- Example 25: Combining Address and Substitution
- Example 26: Deleting Blank Lines
- Example 27: Stripping Leading Whitespace
- Example 28: Stripping Trailing Whitespace
Intermediate (Examples 29–56)
- Example 29: Capture Groups and
\1Backreferences - Example 30: Reformatting Dates with Backreferences
- Example 31: Extended Regex with
-E - Example 32: Alternation with
| - Example 33: POSIX Character Classes
- Example 34: Negated Address with
! - Example 35: Negating a Line Range
- Example 36: Save to Hold Space with
hand Retrieve withg - Example 37: Accumulate with
HandG - Example 38: Exchange Pattern and Hold Space with
x - Example 39: Append Next Line with
N - Example 40: Print First Line of Multiline Pattern Space with
P - Example 41: Delete First Line of Multiline Pattern Space with
D - Example 42: Unconditional Branch with
b - Example 43: Labels and Conditional Branch with
t - Example 44: Negated Conditional Branch with
T - Example 45: Step Addressing with
first~step - Example 46: Address with
0,/regex/ - Example 47: Removing Duplicate Consecutive Lines
- Example 48: Printing Lines Between Two Patterns
- Example 49: Counting Lines (Using
=) - Example 50: Reversing File Lines
- Example 51: Converting Windows Line Endings to Unix
- Example 52: Inserting a Line After a Match
- Example 53: Deleting Lines Matching a Range of Patterns
- Example 54: Multiple Commands in a Block
{} - Example 55: Trimming Whitespace from Both Ends
- Example 56: Wrapping Lines in Quotes
Advanced (Examples 57–85)
- Example 57: GNU sed vs BSD sed In-Place Editing
- Example 58: Extended Regex: GNU
-Evs BSD-E - Example 59: Multiline Pattern Matching Across Line Boundaries
- Example 60: Sliding Window with
Nfor Context - Example 61: Removing a Block When Start and End Are on Adjacent Lines
- Example 62: Extracting a Config Value
- Example 63: Updating a Config Value In-Place
- Example 64: Commenting Out a Config Line
- Example 65: Uncommenting a Config Line
- Example 66: Extracting Fields from Log Lines
- Example 67: Filtering Log Lines by HTTP Status Code
- Example 68: Normalizing Timestamp Formats in Logs
- Example 69: Extracting a CSV Field by Column Position
- Example 70: Adding a CSV Header
- Example 71: Stripping HTML Tags
- Example 72: Extracting Simple JSON Field Values
- Example 73: Sed in a Pipeline Chain
- Example 74: Sed with
find -exec - Example 75: Sed with Heredoc Input
- Example 76: Environment Variable Interpolation in Sed
- Example 77: Alternative Delimiters
- Example 78: Early Exit for Large Files
- Example 79: Processing Only a Line Range for Performance
- Example 80: Block Commenting Multiple Lines
- Example 81: Uncommenting a Block
- Example 82: Sed One-Liner Collection for Common Tasks
- Example 83: Real-World: Patching a Version String in Multiple Files
- Example 84: Real-World: Anonymizing Log Data
- Example 85: Real-World: Generating a Configuration File from a Template
Last updated March 31, 2026