sed Operations Reference

1. Overview

sed (stream editor) performs text transformations on input streams. Essential for configuration file modifications, log parsing, and automation scripts.

Golden Rule: Preview before apply.

Always use -n with p to preview changes before using -i to modify files in place.

2. Core Concepts

2.1. Syntax

sed [options] 'command' file
sed [options] -e 'cmd1' -e 'cmd2' file
sed [options] -f script.sed file

2.2. Common Options

Option Description

-n

Suppress automatic printing (use with p command)

-i

Edit file in place (destructive)

-i.bak

Edit in place, create backup with .bak extension

-e

Add multiple commands

-r / -E

Extended regex (ERE) - enables +, ?, |, ()

3. Substitution (s command)

The most common sed operation.

3.1. Basic Syntax

sed 's/pattern/replacement/flags'

3.2. Flags

Flag Description

g

Global - replace all occurrences on line (not just first)

p

Print - output the modified line

i / I

Case-insensitive matching

1, 2, etc.

Replace only Nth occurrence

4. Preview vs Apply Pattern

This is critical. Never run sed -i without previewing first.

4.1. Step 1: Preview (see what will change)

sed -n 's/old-value/new-value/gp' /path/to/file
  • -n suppresses normal output

  • p flag prints only lines that matched/changed

  • File is NOT modified

Example: Preview DC hostname change
sed -n 's/dc-01\.inside\.domusdigitalis\.dev/home-dc01.inside.domusdigitalis.dev/gp' /etc/krb5.conf
Expected output (only changed lines shown):
        kdc = home-dc01.inside.domusdigitalis.dev
        admin_server = home-dc01.inside.domusdigitalis.dev

4.2. Step 2: Apply (modify the file)

sed -i 's/old-value/new-value/g' /path/to/file
Example: Apply DC hostname change
sudo sed -i 's/dc-01\.inside\.domusdigitalis\.dev/home-dc01.inside.domusdigitalis.dev/g' /etc/krb5.conf

4.3. Step 3: Verify

cat /path/to/file
# or
grep "new-value" /path/to/file

5. Escaping Special Characters

5.1. Characters That Need Escaping

In basic regex (BRE), escape these:

Character Escape As

. (any char)

\.

* (zero or more)

\*

[ ] (char class)

\[ \]

^ (start of line)

\^

$ (end of line)

\$

/ (delimiter)

\/ or use different delimiter

\ (escape)

\\

Example: Escaping dots in domain names
# WRONG - dots match any character
sed -n 's/dc-01.inside.domusdigitalis.dev/new/gp' file

# CORRECT - dots are literal
sed -n 's/dc-01\.inside\.domusdigitalis\.dev/new/gp' file

5.2. Alternative Delimiters

When pattern contains /, use different delimiter:

# Default delimiter (awkward with paths)
sed 's/\/home\/user\/old/\/home\/user\/new/g'

# Better - use | or # as delimiter
sed 's|/home/user/old|/home/user/new|g'
sed 's#/home/user/old#/home/user/new#g'

6. Address Ranges

Limit operations to specific lines.

6.1. Line Numbers

# Line 5 only
sed -n '5p' file

# Lines 5-10
sed -n '5,10p' file

# From line 5 to end
sed -n '5,$p' file

# First line only
sed -n '1p' file

# Last line only
sed -n '$p' file

6.2. Pattern Matching

# Lines containing "error"
sed -n '/error/p' file

# Lines between two patterns (inclusive)
sed -n '/START/,/END/p' file

# Delete lines matching pattern
sed '/pattern/d' file

7. Common Operations

7.1. Delete Lines

# Delete lines containing pattern
sed '/pattern/d' file

# Delete empty lines
sed '/^$/d' file

# Delete lines 1-5
sed '1,5d' file

# Delete last line
sed '$d' file

7.2. Insert and Append

# Insert before line 3
sed '3i\New line text' file

# Append after line 3
sed '3a\New line text' file

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

7.3. Multiple Operations

# Using -e for multiple commands
sed -e 's/foo/bar/g' -e 's/baz/qux/g' file

# Using semicolons
sed 's/foo/bar/g; s/baz/qux/g' file

8. Real-World Examples

8.1. Configuration File Updates

Update Kerberos DC reference
# Preview
sed -n 's/dc-01\.inside\.domusdigitalis\.dev/home-dc01.inside.domusdigitalis.dev/gp' /etc/krb5.conf

# Apply
sudo sed -i 's/dc-01\.inside\.domusdigitalis\.dev/home-dc01.inside.domusdigitalis.dev/g' /etc/krb5.conf
Update IP address in config
# Preview
sed -n 's/10\.50\.1\.50/10.50.1.51/gp' /etc/hosts

# Apply
sudo sed -i 's/10\.50\.1\.50/10.50.1.51/g' /etc/hosts

8.2. Comment/Uncomment Lines

# Comment out a line (add # at start)
sed -i 's/^PasswordAuthentication yes/#PasswordAuthentication yes/' /etc/ssh/sshd_config

# Uncomment a line (remove # at start)
sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication yes/' /etc/ssh/sshd_config

# Comment all lines matching pattern
sed -i '/pattern/s/^/#/' file

8.3. Strip Whitespace

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

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

# Remove both
sed 's/^[ \t]*//; s/[ \t]*$//' file

8.4. Extract Values

# Extract IP addresses (basic)
sed -n 's/.*\([0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\).*/\1/p' file

# Extract value after = sign
sed -n 's/^key=\(.*\)/\1/p' file

9. Backup Strategy

9.1. Create Backup with sed

# Create .bak backup before editing
sed -i.bak 's/old/new/g' file

# Creates: file (modified) and file.bak (original)

9.2. Manual Backup First

# Recommended for critical files
cp /etc/krb5.conf /etc/krb5.conf.$(date +%Y%m%d)
sed -i 's/old/new/g' /etc/krb5.conf

10. Troubleshooting

10.1. Nothing Changed?

  1. Pattern didn’t match - Check escaping, case sensitivity

  2. Forgot -i flag - sed outputs to stdout by default

  3. Wrong file - Verify path

Debug: Show what sed sees
# Print all lines, mark matches
sed -n 's/pattern/[MATCH: &]/gp' file

10.2. Unexpected Changes?

  1. Greedy matching - .* matches as much as possible

  2. Missing g flag - Only first occurrence per line replaced

  3. Unescaped special chars - Dots, asterisks, etc.

11. Quick Reference

Operation Command

Preview substitution

sed -n 's/old/new/gp' file

Apply substitution

sed -i 's/old/new/g' file

Apply with backup

sed -i.bak 's/old/new/g' file

Delete matching lines

sed '/pattern/d' file

Delete empty lines

sed '/^$/d' file

Print matching lines

sed -n '/pattern/p' file

Print line range

sed -n '5,10p' file

Comment line

sed -i 's/^pattern/#pattern/' file

Replace in place (global)

sed -i 's/old/new/g' file

Multiple substitutions

sed -e 's/a/b/g' -e 's/c/d/g' file

  • sed Deep Dive (linux-ops) - Comprehensive sed reference (hold buffer, branching, production scenarios)

  • Text Processing (linux-ops) - awk, cut, tr, sort and pipeline mastery

  • Git Commands

  • Domain Join / DC Migration (ise-linux)