โ† All cheat sheets

Sed

Authorized use only. Offensive reference for systems you own or are explicitly permitted to test. You are responsible for staying within the law.

BASIC SYNTAX#

sed 'command' file                  # Apply command to file
sed -e 'cmd1' -e 'cmd2' file        # Multiple commands
sed -f script.sed file              # Commands from file
sed -n 'command' file               # Suppress default output
sed -i 'command' file               # Edit file in place
sed -i.bak 'command' file           # In place with backup

SUBSTITUTE COMMAND (s)#

sed 's/old/new/' file               # Replace first occurrence per line
sed 's/old/new/g' file              # Replace all occurrences
sed 's/old/new/2' file              # Replace 2nd occurrence
sed 's/old/new/gi' file             # Replace all, case insensitive
sed 's/old/new/p' file              # Print substituted lines
sed -n 's/old/new/p' file           # Print only changed lines
sed 's/old/new/w output' file       # Write changed lines to file

DELIMITERS#

sed 's/old/new/'                    # Forward slash (default)
sed 's|old|new|'                    # Pipe
sed 's#old#new#'                    # Hash
sed 's@old@new@'                    # At sign
sed 's:old:new:'                    # Colon

# Useful when pattern contains slashes
sed 's|/usr/local|/opt|g' file

ADDRESS SELECTION#

sed '5s/old/new/' file              # Line 5 only
sed '1,5s/old/new/' file            # Lines 1-5
sed '5,$s/old/new/' file            # Line 5 to end
sed '/pattern/s/old/new/' file      # Lines matching pattern
sed '/start/,/end/s/old/new/' file  # Between patterns
sed '0,/pattern/s/old/new/' file    # First occurrence only
sed '1!s/old/new/' file             # All lines except first
sed '/pattern/!s/old/new/' file     # Lines NOT matching pattern

DELETE COMMAND (d)#

sed 'd' file                        # Delete all lines
sed '5d' file                       # Delete line 5
sed '1,5d' file                     # Delete lines 1-5
sed '5,$d' file                     # Delete line 5 to end
sed '/pattern/d' file               # Delete matching lines
sed '/^$/d' file                    # Delete blank lines
sed '/^#/d' file                    # Delete comment lines
sed '1d;$d' file                    # Delete first and last
sed '/start/,/end/d' file           # Delete between patterns
sed -n 'p' file                     # Print all lines
sed -n '5p' file                    # Print line 5
sed -n '1,5p' file                  # Print lines 1-5
sed -n '/pattern/p' file            # Print matching lines
sed -n '/start/,/end/p' file        # Print between patterns
sed -n '5,10p' file                 # Print lines 5-10

INSERT/APPEND/CHANGE#

sed '5i\New line' file              # Insert before line 5
sed '5a\New line' file              # Append after line 5
sed '5c\Replacement' file           # Change line 5
sed '/pattern/i\New line' file      # Insert before match
sed '/pattern/a\New line' file      # Append after match
sed '/pattern/c\Replacement' file   # Change matching lines

# Multi-line insert (GNU sed)
sed '5i\Line 1\nLine 2' file

TRANSFORM COMMAND (y)#

sed 'y/abc/ABC/' file               # Transform characters
sed 'y/aeiou/AEIOU/' file           # Vowels to uppercase
sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' file

HOLD SPACE COMMANDS#

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

MULTIPLE COMMANDS#

sed 's/a/A/; s/b/B/' file           # Semicolon separated
sed -e 's/a/A/' -e 's/b/B/' file    # Multiple -e options
sed '
s/a/A/
s/b/B/
' file                              # Multi-line

# Using braces for grouping
sed '/pattern/{s/old/new/; s/foo/bar/}' file

REGULAR EXPRESSIONS#

# Basic metacharacters
.                                   # Any single character
*                                   # Zero or more
^                                   # Start of line
$                                   # End of line
[]                                  # Character class
[^]                                 # Negated class
\                                   # Escape character

# Extended regex (-E or -r)
sed -E 's/[0-9]+/NUM/g' file
+                                   # One or more
?                                   # Zero or one
{n,m}                               # n to m times
|                                   # Alternation
()                                  # Grouping

BACKREFERENCES#

sed 's/\(word\)/[\1]/' file         # Basic: \( \) and \1
sed -E 's/(word)/[\1]/' file        # Extended: ( ) and \1
sed 's/\([a-z]*\) \([a-z]*\)/\2 \1/' file  # Swap words

# Named groups (GNU sed)
sed -E 's/(?P<name>pattern)/\g<name>/' file

PRACTICAL EXAMPLES#

# Remove leading whitespace
sed 's/^[ \t]*//' file

# Remove trailing whitespace
sed 's/[ \t]*$//' file

# Remove leading and trailing whitespace
sed 's/^[ \t]*//; s/[ \t]*$//' file

# Remove blank lines
sed '/^$/d' file

# Remove empty lines and lines with only whitespace
sed '/^[[:space:]]*$/d' file

# Remove comments (lines starting with #)
sed '/^#/d' file

# Remove inline comments
sed 's/#.*$//' file

# Add line numbers
sed = file | sed 'N; s/\n/\t/'

# Double-space file
sed 'G' file

# Number non-blank lines
sed '/./=' file | sed '/./N; s/\n/ /'

# Reverse lines (like tac)
sed '1!G; h; $!d' file

# Print first 10 lines (like head)
sed '10q' file

# Print last 10 lines (like tail)
sed -n '$-9,$p' file                # Not portable
sed ':a; $q; N; 11,$D; ba' file     # Portable

# Convert DOS to Unix line endings
sed 's/\r$//' file

# Convert Unix to DOS line endings
sed 's/$/'$'\r''/g' file

# Insert line after match
sed '/pattern/a\New line' file

# Insert line before match
sed '/pattern/i\New line' file

# Extract email addresses
sed -n 's/.*\([a-zA-Z0-9._%+-]*@[a-zA-Z0-9.-]*\.[a-zA-Z]*\).*/\1/p' file

# Comment out lines matching pattern
sed '/pattern/s/^/#/' file

# Uncomment lines
sed 's/^#//' file

# Wrap lines in quotes
sed 's/.*/"&"/' file

# Add prefix/suffix to lines
sed 's/^/PREFIX/' file
sed 's/$/SUFFIX/' file

# Convert lowercase to uppercase
sed 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/' file

# GNU sed: convert case
sed 's/.*/\U&/' file                # To uppercase
sed 's/.*/\L&/' file                # To lowercase
sed 's/\b./\u&/g' file              # Capitalize words

IN-PLACE EDITING#

# Edit file in place (GNU sed)
sed -i 's/old/new/g' file

# In place with backup
sed -i.bak 's/old/new/g' file

# macOS/BSD sed requires empty string for no backup
sed -i '' 's/old/new/g' file

# Multiple files
sed -i 's/old/new/g' file1 file2 file3
sed -i 's/old/new/g' *.txt

BRANCHING & FLOW CONTROL#

b                                   # Branch to end/label
t                                   # Branch if substitution
T                                   # Branch if no substitution (GNU)
:label                              # Define label
n                                   # Read next line
N                                   # Append next line
q                                   # Quit

# Example: print line and next
sed -n '/pattern/{p; n; p}' file

# Example: quit after first match
sed '/pattern/q' file

SED SCRIPTS#

# Create script file (script.sed)
#!/bin/sed -f
s/old/new/g
/pattern/d
1,10s/foo/bar/

# Run script
sed -f script.sed file

OPTIONS SUMMARY#

-n                                  # Suppress automatic printing
-e                                  # Add script/command
-f                                  # Read commands from file
-i                                  # In-place edit
-i.bak                              # In-place with backup
-r, -E                              # Extended regex
-s                                  # Treat files separately
--                                  # End of options

GNU vs BSD/macOS DIFFERENCES#

# In-place without backup
GNU:    sed -i 's/old/new/' file
BSD:    sed -i '' 's/old/new/' file

# Extended regex
GNU:    sed -r or sed -E
BSD:    sed -E

# Case conversion
GNU:    \U \L \u \l (uppercase/lowercase)
BSD:    Not available