Originally published on 2010-11-23; fully revised and updated.
sed is a non-interactive stream editor used to process text line by line. This guide is for DevOps engineers and sysadmins who need fast, repeatable ways to select or remove lines in logs, config files, and shell pipelines.

What sed does in Unix automation
sed reads from standard input or a file, applies commands, and writes to standard output. Internally, it works with a pattern space for the current line and an optional hold space for temporary storage across cycles.
- Use
pto print selected lines (usually with-n). - Use
dto delete selected lines from output. - Use addresses (line numbers, ranges, regex) to target exactly what should be processed.
How a sed execution cycle works
sed processes input one cycle at a time: read a line into pattern space, run commands, print (unless suppressed), then continue. If d is executed, sed skips printing for that cycle and immediately moves to the next line.
flowchart TD
A[Start cycle] --> B[Read next line into pattern space]
B --> C[Apply addressed commands]
C --> D{Did d command run}
D -- yes --> E[Skip printing and continue]
D -- no --> F{Is auto print active}
F -- yes --> G[Print pattern space]
F -- no --> H[Only explicit p prints]
E --> I{More input lines}
G --> I
H --> I
I -- yes --> B
I -- no --> J[Exit]
Create the sample file used in these examples
cat > sample.txt <<'EOF'
1. Linux - Sysadmin, Scripting etc.
2. Databases - Oracle, mySQL etc.
3. Hardware
4. Security (Firewall, Network, Online Security etc)
5. Storage
6. Cool gadgets and websites
7. Productivity (Too many technologies to explore)
8. Website Design
9. Software Development
10.Windows- Sysadmin, reboot etc.
EOF
nl -ba sample.txtCode language: JavaScript (javascript)
Print targeted lines with -n and p
When using p, pass -n to suppress automatic printing; otherwise matching lines can appear twice.
| Address form | Example | What it selects | Portability |
|---|---|---|---|
N | sed -n '3p' sample.txt | Only line N | POSIX |
M~N | sed -n '3~2p' sample.txt | Every Nth line starting at M | GNU extension |
M,N | sed -n '4,8p' sample.txt | Inclusive numeric range | POSIX |
$ | sed -n '$p' sample.txt | Last line | POSIX |
N,$ | sed -n '4,$p' sample.txt | From line N to end | POSIX |
/PATTERN/ | sed -n '/Sysadmin/p' sample.txt | Lines matching regex | POSIX |
/PATTERN/,N | sed -n '/Hardware/,6p' sample.txt | From first regex match through line N | POSIX |
N,/PATTERN/ | sed -n '3,/Security/p' sample.txt | From line N through first later regex match | POSIX |
/PATTERN/,$ | sed -n '/Website/,$p' sample.txt | From regex match to end | POSIX |
/PATTERN/,+N | sed -n '/Storage/,+2p' sample.txt | Matched line plus next N lines | GNU extension |
/P1/,/P2/ | sed -n '/Storage/,/Design/p' sample.txt | Range between two regex matches | POSIX |
# Print exactly line 3
sed -n '3p' sample.txt
# 3. Hardware
# Print every second line starting from line 3 (GNU sed)
sed -n '3~2p' sample.txt
# 3. Hardware
# 5. Storage
# 7. Productivity (Too many technologies to explore)
# 9. Software Development
# Print lines containing Sysadmin
sed -n '/Sysadmin/p' sample.txt
# 1. Linux - Sysadmin, Scripting etc.
# 10.Windows- Sysadmin, reboot etc.
# Print from Storage through Design (inclusive)
sed -n '/Storage/,/Design/p' sample.txt
# 5. Storage
# 6. Cool gadgets and websites
# 7. Productivity (Too many technologies to explore)
# 8. Website DesignCode language: PHP (php)
Delete lines with d using the same address patterns
d is effectively the inverse of p for many workflows: it removes matching lines from output and immediately starts the next sed cycle.
| Goal | Print selector | Delete selector |
|---|---|---|
| Nth line | sed -n '3p' sample.txt | sed '3d' sample.txt |
| Range of lines | sed -n '4,8p' sample.txt | sed '4,8d' sample.txt |
| Last line | sed -n '$p' sample.txt | sed '$d' sample.txt |
| Regex matches | sed -n '/Sysadmin/p' sample.txt | sed '/Sysadmin/d' sample.txt |
| Match to end of file | sed -n '/Website/,$p' sample.txt | sed '/Website/,$d' sample.txt |
| Blank lines | sed -n '/^$/p' sample.txt | sed '/^$/d' sample.txt |
# Delete line 3
sed '3d' sample.txt
# Delete lines 4 through 8
sed '4,8d' sample.txt
# Delete all lines containing Sysadmin
sed '/Sysadmin/d' sample.txt
# Delete from Website to end of file
sed '/Website/,$d' sample.txt
# Delete blank lines
sed '/^$/d' sample.txtCode language: PHP (php)
GNU sed extensions and portability notes
Most basic address forms are portable, but some compact selectors are GNU-only. If your scripts run across Linux and macOS/BSD systems, validate syntax before production rollout.
| Feature | POSIX sed | GNU sed | BSD/macOS sed |
|---|---|---|---|
Basic addresses (N, M,N, $, /re/) | Yes | Yes | Yes |
M~N step addressing | No | Yes | No |
/re/,+N relative range | No | Yes | No |
-i in-place edit without suffix | Not standardized | Yes | No (use empty or explicit suffix) |
# Cross-platform friendly in-place edit with backup
sed -i.bak '/^$/d' sample.txt
# Portable fallback without -i
sed '/^$/d' sample.txt > sample.txt.tmp && mv sample.txt.tmp sample.txtCode language: PHP (php)
Best practices
- Start with read-only commands (
-n+p) before applying destructive edits. - Prefer single-quoted sed scripts in shell to avoid accidental variable expansion.
- Remember sed uses basic regular expressions by default.
- Use backups (
-i.bak) or temp files for safe bulk edits. - Document GNU-only syntax in scripts to prevent cross-platform surprises.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals