Skip to content

Sed Command Cheatsheet

Table of Contents

Basic Syntax

Terminal window
sed [options] 'command' file
sed [options] -e 'command1' -e 'command2' file
sed [options] -f script-file file
  • options: Modify sed behavior
  • command: Operation to perform
  • file: Input file (optional, can use stdin)

Address Types

Terminal window
sed '1d' # First line
sed '$d' # Last line
sed '1,3d' # Lines 1-3
sed '2,+4d' # Line 2 and next 4 lines
sed '1~2d' # Every odd line
sed '2~2d' # Every even line

Basic Commands

Terminal window
p # Print line
d # Delete line
q # Quit sed
n # Print line and read next
a # Append text
i # Insert text
c # Change/replace line
= # Print line number
l # Print line unambiguously

Text Replacement

Terminal window
# Basic syntax
s/pattern/replacement/[flags]
# Common flags
g # Global (all occurrences)
1-9 # Nth occurrence only
p # Print if substitution made
w # Write to file if substitution made
I # Case insensitive

Advanced Commands

Hold Space Operations

Terminal window
h # Copy pattern space to hold space
H # Append pattern space to hold space
g # Copy hold space to pattern space
G # Append hold space to pattern space
x # Exchange pattern and hold spaces

Flow Control

Terminal window
:label # Define label
b label # Branch to label
t label # Branch on substitute
T label # Branch if no substitute

Regular Expressions

Terminal window
. # Any single character
^ # Start of line
$ # End of line
* # Zero or more occurrences
\+ # One or more occurrences
\? # Zero or one occurrence
[] # Character class
[^] # Negated character class

Multiple Commands

Using Multiple Commands

Terminal window
# Using semicolons
sed 's/old/new/g; s/next/prev/g' file.txt
# Using -e option
sed -e 's/old/new/g' -e 's/next/prev/g' file.txt
# Using a script file
sed -f commands.sed file.txt

File Operations

Terminal window
# In-place editing
sed -i 's/old/new/g' file.txt
# Backup original file
sed -i.bak 's/old/new/g' file.txt
# Write specific lines to file
sed -n '/pattern/w output.txt' file.txt

Practical Examples

Text Processing

Terminal window
# Remove empty lines
sed '/^$/d' file.txt
# Remove comments and empty lines
sed -e '/^#/d' -e '/^$/d' file.txt
# Add line numbers
sed = file.txt | sed 'N;s/\n/\t/'
# Double space output
sed G file.txt

Configuration File Editing

Terminal window
# Change configuration value
sed -i 's/^SETTING=.*/SETTING=newvalue/' config.txt
# Comment out lines
sed -i 's/^[^#]/# &/' file.txt
# Uncomment lines
sed -i 's/^# //' file.txt

Advanced Text Manipulation

Terminal window
# Reverse line order
sed -n '1!G;h;$p' file.txt
# Remove trailing whitespace
sed 's/[[:space:]]*$//' file.txt
# Convert DOS to Unix
sed 's/\r$//' file.txt

Advanced Techniques

Using Labels and Branches

Terminal window
# Conditional processing
sed ':start
/pattern/ {
s/old/new/
t start
}' file.txt
# Skip blocks of text
sed '/start/,/end/b' file.txt

Using Hold Space

Terminal window
# Reverse paragraphs
sed -e '/./{H;$!d;}' -e 'x;/^\n/d' file.txt
# Duplicate lines
sed 'h;p' file.txt
# Swap lines
sed 'N;s/\(.*\)\n\(.*\)/\2\n\1/' file.txt

Tips and Best Practices

  1. Debugging

    Terminal window
    # Print commands as executed
    sed -n 'l' file.txt
    # Use echo for testing
    echo "test" | sed 's/test/working/'
  2. Performance

    Terminal window
    # Use -E for complex patterns
    sed -E 's/(complex|pattern)/simple/g'
    # Combine multiple operations
    sed 's/one/1/g; s/two/2/g'
  3. Safety

    Terminal window
    # Make backups
    sed -i.bak 's/old/new/g' file.txt
    # Test commands before running
    sed -n 's/pattern/replacement/gp' file.txt