How do I delete one line or a range?
sed '7d' hosts.txt # line 7 sed '3,9d' hosts.txt # lines 3 to 9 sed '20,$d' hosts.txt # line 20 to the end sed '1d;$d' report.csv # first and last line
Without -i, sed prints the result and leaves the file unchanged, which is how every deletion should start. The semicolon separates commands, so 1d;$d removes a header and a footer in one pass.
How do I delete lines matching a pattern?
sed '/^DEBUG/d' app.log sed '/healthz/d; /favicon.ico/d' access.log sed -E '/(kube-probe|UptimeCheck)/d' access.log
Any regex works as an address. Anchors matter: /^DEBUG/ removes lines starting with DEBUG, while /DEBUG/ removes every line that contains it anywhere.
How do I keep only matching lines?
Negate the address with !, which deletes everything that does not match:
sed '/ERROR\|CRITICAL/!d' worker.log sed -E '/^(GET|POST) /!d' requests.log
This is the same result as grep for that pattern, useful inside longer sed scripts where you want to filter and edit in one tool.
How do I remove blank lines and comments?
| Goal | Command |
|---|---|
| Empty lines | sed '/^$/d' |
| Lines with only spaces or tabs | sed '/^[[:space:]]*$/d' |
| Comment lines starting with # | sed '/^[[:space:]]*#/d' |
| Comments and blanks together | sed -E '/^[[:space:]]*(#|$)/d' |
| Trailing comments after settings | sed 's/[[:space:]]*#.*$//' |
The last row edits rather than deletes; be careful with it in files where # can appear inside values, such as passwords or URLs with fragments.
How do I delete a block between two markers?
sed '/# BEGIN legacy-redirects/,/# END legacy-redirects/d' /etc/nginx/conf.d/shop.conf
Everything from the first marker line through the second, inclusive, disappears. Scripts that manage part of a shared file often write such markers so the block can be replaced or removed cleanly later. To keep the markers and delete only what lies between them, add an inner condition: sed '/BEGIN x/,/END x/{//!d}', where // reuses the last matched pattern.
How do I delete the last few lines?
sed processes one line at a time and does not know the file length ahead of time, so "last N lines" is easier with head:
head -n -3 export.csv # all but the last 3 lines (GNU head)
sed -n -e :a -e '1,3!{P;N;D;};N;ba' export.csv # the same in pure sed
The pure sed version works but is hard to read; prefer head -n -N on Linux. FreeBSD's head does not accept negative counts, so use tail -r | tail -n +4 | tail -r there.
How do I apply the deletion to the file?
sed -i.bak '/^#.*legacy/d' /etc/php/8.3/fpm/pool.d/www.conf diff -u /etc/php/8.3/fpm/pool.d/www.conf.bak /etc/php/8.3/fpm/pool.d/www.conf php-fpm8.3 -t
Keep the .bak until the service has been tested and reloaded, then remove it so stale copies do not pile up in configuration folders. Some services read every file in a folder, and a forgotten backup with a matching extension can be loaded as live configuration.
Where can I learn the rest of sed?
Substitutions, insertions and addresses are covered in The sed command. For deleting lines interactively inside an editor, Vim's :g/pattern/d does the same job, as shown in Find and replace in Vim.
How do I delete duplicate lines?
sed can remove consecutive duplicates with sed '$!N; /^\(.*\)\n\1$/!P; D', but the readable tools are better: uniq file for neighbors, and awk '!seen[$0]++' file to drop every repeat while keeping the original order. For duplicates inside a database table rather than a file, see Find duplicate rows in MySQL.
How do I delete lines from a log that is still being written?
Do not edit it in place. sed -i writes a new file and renames it, while the logging process keeps writing to the old, now deleted, file. Filter a copy instead, or let logrotate handle the live log.
Keep Reading
The sed command
Edit text streams and files with sed: substitutions, in-place edits with backups, line addresses, extended regex, multiple commands and GNU versus BSD.
OpenExclude with grep
Leave things out of grep results: invert matches, skip files and folders by pattern, ignore binaries and comments, and filter noisy log lines.
OpenFind and replace in Vim
Search and substitute in Vim: the :s command, ranges, flags, whole words, regex groups, visual selections and changes across many files.
OpenThe grep command
grep from basics to power use: case, whole words, line numbers, context, recursion, regex flavors, only-matching output and speed tips.
OpenFAQ
Quick Answers
Use the line number with d, such as sed '7d' file.
Use a pattern address, such as sed '/word/d' file.
Use sed '/^$/d', or sed '/^[[:space:]]*$/d' to include lines with only spaces.
Negate the address: sed '/pattern/!d' file.
Add -i, ideally -i.bak to keep a backup, and check the result with diff.