Text Processing with Sed
Objective
Use sed as a stream editor to perform substitution, deletion, insertion, and line selection on text files and streams.
Tools & Technologies
sedstream editorregex substitution
Key Commands
sed 's/old/new/g' filesed -i.bak 's/foo/bar/g' filesed -n '5,10p' filesed '/^#/d' configArchitecture Overview
flowchart LR
INPUT[Input\nline by line] --> READ[Read line\nto pattern space]
READ --> MATCH{Pattern\nmatches?}
MATCH -->|yes| ACTION[Apply\ncommand]
MATCH -->|no| OUTPUT
ACTION --> OUTPUT[Print to\nstdout]
OUTPUT --> READ
OUTPUT -->|EOF| DONE[Done]
style INPUT fill:#1a1a2e,stroke:#00d4ff,color:#e0e0e0
style ACTION fill:#1a1a2e,stroke:#00ff88,color:#e0e0e0
Step-by-Step Process
01
Substitution
The s command is the most-used sed operation. Format: s/find/replace/flags
sed 's/foo/bar/' file # first match per line
sed 's/foo/bar/g' file # all matches (global)
sed 's/foo/bar/2' file # second match only
sed 's/foo/bar/gi' file # global + case insensitive
# In-place edit with backup
sed -i.bak 's/old/new/g' file
# Remove trailing whitespace
sed 's/[[:space:]]*$//' file
02
Line Selection
Apply commands only to specific lines using line numbers, ranges, or patterns.
sed -n '5p' file # print line 5 only
sed -n '5,10p' file # print lines 5-10
sed '1d' file # delete line 1
sed '1,5d' file # delete lines 1-5
sed '/^#/d' config # delete comment lines
sed -n '/START/,/END/p' # print between patterns
03
Insert & Append
Add lines before or after matched patterns.
# Insert line before pattern
sed '/pattern/i\New line before' file
# Append line after pattern
sed '/pattern/a\New line after' file
# Insert at line number
sed '3i\Inserted at line 3' file
04
Practical Use Cases
Real sed one-liners for config management and data transformation.
# Comment out a line
sed 's/^PermitRootLogin/# &/' /etc/ssh/sshd_config
# Uncomment a line
sed 's/^# *PasswordAuthentication/PasswordAuthentication/' sshd_config
# Remove blank lines
sed '/^$/d' file
# Add line numbers
sed = file | sed 'N;s/\n/\t/'
Challenges & Solutions
- sed -i without backup is destructive — always use -i.bak first
- GNU sed -i differs from BSD sed -i — macOS requires -i ''
Key Takeaways
- sed is ideal for single-pass transformations; awk is better for structured data
- Test sed patterns without -i first to verify output