sed d: Delete Lines from a Stream
sed d removes lines from the output, selected by line number, range, or a regex address.
Deleting is the inverse of printing. Pair the d command with an address and sed drops exactly the lines you name.
What it does
The d command deletes the current line so it never reaches output. With no address it deletes every line; with an address it deletes only the matching lines. d ends processing of that line, so commands after it do not run on deleted lines.
Common usage
sed '3d' file # delete line 3
sed '2,5d' file # delete lines 2 through 5
sed '/^#/d' config.ini # delete comment lines
sed '/^$/d' file # delete blank lines
sed '$d' file # delete the last lineOptions
| Address | What it deletes |
|---|---|
| Nd | The single line number N |
| M,Nd | Lines M through N inclusive |
| /regex/d | Every line matching the regex |
| $d | The last line of input |
| /a/,/b/d | From the first line matching a to the next matching b |
| Nd with -n | Has no extra effect; d already suppresses the line |
In CI
Deleting comment and blank lines is a common way to normalize a config before a diff or checksum. The d syntax is identical on GNU and BSD sed, so deletes are safe in cross-platform jobs as long as the address regex itself is portable.
Common errors in CI
sed: -e expression #1, char N: unexpected ,' means a malformed range such as ,5d with no start. sed: -e expression #1, char N: unknown command: d' can appear if a stray character precedes d, for example a missing slash on the address making sed read the pattern as a command.