sed Stream Editor

sed cheatsheet — stream editor for find-and-replace and text transforms. sed 's/old/new/g', sed -i in-place edit, sed '/pattern/d' delete lines. Linux text processing tool.

6 min read

What it is

sed is a stream editor that parses and transforms text, typically used for find-and-replace operations on files or piped input.

Installation

Linux

sed is usually pre-installed on most Linux distributions. If not:

sudo apt update && sudo apt install sed # Debian/Ubuntu
sudo yum install sed                 # CentOS/RHEL
sudo dnf install sed                 # Fedora

macOS

sed is pre-installed on macOS.

Windows

sed is not natively included. You can get it via:

  • Git Bash: If you have Git for Windows installed, sed is available in the Git Bash terminal.
  • Cygwin: Install the sed package through the Cygwin installer.
  • WSL (Windows Subsystem for Linux): Install a Linux distribution (like Ubuntu) from the Microsoft Store, and sed will be available within that Linux environment.

Commands / Usage

Basic Substitution

Replace the first occurrence of old_text with new_text in each line:

sed 's/old_text/new_text/' input.txt

Replace all occurrences of old_text with new_text in each line:

sed 's/old_text/new_text/g' input.txt

Replace all occurrences of old_text with new_text in each line, ignoring case:

sed 's/old_text/new_text/gi' input.txt

Replace a character (e.g., /) with another character (e.g., -) by escaping the delimiter or using a different delimiter:

sed 's/\//-/' input.txt
sed 's|/|-|' input.txt

In-place Editing

Modify the file directly (use with caution!):

sed -i 's/old_text/new_text/g' input.txt

Create a backup of the original file before modifying:

sed -i.bak 's/old_text/new_text/g' input.txt

Addressing Lines

Apply substitution only to lines matching a pattern:

sed '/pattern_to_match/s/old_text/new_text/g' input.txt

Apply substitution only to lines NOT matching a pattern:

sed '/pattern_to_match/!s/old_text/new_text/g' input.txt

Apply substitution to a range of lines (line 5 to line 10):

sed '5,10s/old_text/new_text/g' input.txt

Apply substitution from a line matching a pattern to another line matching a pattern:

sed '/start_pattern/,/end_pattern/s/old_text/new_text/g' input.txt

Apply substitution to a specific line number:

sed '5s/old_text/new_text/g' input.txt

Apply substitution to all lines from a specific line number to the end of the file:

sed '5,$s/old_text/new_text/g' input.txt

Deleting Lines

Delete lines matching a pattern:

sed '/pattern_to_delete/d' input.txt

Delete a specific line number:

sed '5d' input.txt

Delete a range of lines:

sed '5,10d' input.txt

Delete empty lines:

sed '/^$/d' input.txt

Printing Lines

Print only lines matching a pattern (default action is to print all lines):

sed -n '/pattern_to_print/p' input.txt

Print lines from line 5 to line 10:

sed -n '5,10p' input.txt

Print all lines except those matching a pattern:

sed -n '/pattern_to_exclude/!p' input.txt

Print the whole file (explicitly, though often redundant):

sed -n 'p' input.txt

Inserting and Appending Text

Insert text before a line matching a pattern:

sed '/pattern_to_insert_before/i\
Inserted text line 1\
Inserted text line 2' input.txt

Append text after a line matching a pattern:

sed '/pattern_to_append_after/a\
Appended text line 1\
Appended text line 2' input.txt

Insert text before a specific line number:

sed '5i\
Inserted text before line 5' input.txt

Append text after a specific line number:

sed '5a\
Appended text after line 5' input.txt

Changing Lines

Replace entire lines matching a pattern with new text:

sed '/pattern_to_change/c\
New text for the entire line' input.txt

Replace a specific line number with new text:

sed '5c\
New text for line 5' input.txt

Branching and Looping (Advanced)

Jump to a label if a pattern is found:

sed ':loop\n /pattern/ b loop\n s/old/new/' input.txt

Branch to a label unconditionally:

sed ':start\n s/foo/bar/\n b start' input.txt

Branch to a label only if the pattern is NOT found:

sed '/pattern/! b end\n s/foo/bar/\n :end' input.txt

Reading and Writing Files

Read the content of another file and insert it after lines matching a pattern:

sed '/pattern_to_read_after/r other_file.txt' input.txt

Write lines matching a pattern to a separate file:

sed -n '/pattern_to_write/w output.txt' input.txt

Using Multiple Commands

Execute multiple sed commands sequentially:

sed -e 's/old1/new1/g' -e 's/old2/new2/g' input.txt

Or separate commands with semicolons:

sed 's/old1/new1/g; s/old2/new2/g' input.txt

Read commands from a file:

sed -f commands.sed input.txt

Common Patterns

Extracting lines between two patterns:

sed -n '/START_PATTERN/,/END_PATTERN/p' input.log

Removing HTML tags:

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

Replacing multiple spaces with a single space:

sed 's/ \+/\ /g' input.txt

Renaming multiple files based on a pattern (using ls and xargs):

ls *.old | sed 's/\(.*\)\.old$/mv \1.old \1.new/' | sh

Note: This is a powerful but potentially dangerous command. Always preview the mv commands first by omitting | sh.

Counting lines matching a pattern:

sed -n '/pattern/=' input.txt | wc -l

Removing leading/trailing whitespace from each line:

sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' input.txt

Replacing a delimiter in a CSV file (e.g., tab to comma):

sed 's/\t/,/g' input.tsv

Escaping special characters in a string for use in sed:

# Example: escape '/' and '.'
echo "path/to/file.txt" | sed 's/[/.]/\\&/g'
# Output: path\/to\/file\.txt

Gotchas

  • In-place editing (-i) is destructive: Always test your sed commands without -i first, or use -i.bak to create backups.
  • Delimiter choice: If your search or replacement string contains the default delimiter (/), you must either escape it (\/) or choose a different delimiter (e.g., |, #, :) like sed 's|old/path|new/path|'.
  • Regular Expression Syntax: sed uses Basic Regular Expressions (BRE) by default. Some advanced features (like +, ?, |, ()) require escaping (\+, \?, \|, \(\)) or using Extended Regular Expressions (ERE) with the -E flag (e.g., sed -E 's/a+|b?/c/').
  • Global flag (g): Without g, sed only replaces the first occurrence on each line.
  • Newline handling: Inserting (i) or appending (a) text requires careful handling of newlines. The text to be inserted/appended usually follows the command character (i, a, c) on the next line, or is escaped (\n) within the same line.
  • Scripting (-f): When using a script file, each command should ideally be on its own line. Comments can be added using #.
  • Exit Status: sed typically returns 0 on success. Non-zero indicates an error (e.g., invalid syntax).
  • Empty Input/Output: If sed receives no input or the pattern doesn’t match, it will output nothing (unless printing is explicitly forced).
  • Order of Operations: When using multiple commands (e.g., -e or ;), they are applied sequentially to each line. The output of one command becomes the input for the next.