Lesson 4
Delete Selected Lines with sed
Objective: Remove selected input lines from the output by address or pattern.
The d instruction deletes the current pattern space and starts the next input cycle. In these single-line examples, the selected line disappears from the output. The input file itself remains unchanged.
sed '/^M/d' petchipThe regular-expression address /^M/ selects lines beginning with uppercase M. The caret is a beginning-of-line anchor, not a wildcard that consumes a character. Maisey, Mitsy, and Milo are omitted; Sam and the other records remain.
Numeric addresses and ranges
sed '5d' petchip
sed '3,5d' petchip
sed '5,$d' petchip
Another useful aspect of the d command is that it can be combined with a wide variety of addresses to filter data quickly and efficiently. The address does not need to be a regular expression; it can also be a specific line number, a range of line numbers, or another pattern match. For example, sed '3d' petchip deletes only the third line of output, while sed '2,5d' petchip suppresses lines two through five. Because sed processes input as a stream, unwanted lines are discarded as they are read, making the command efficient even when working with large files. This behavior makes the d instruction a common choice for cleaning log files, removing header records, or excluding lines that match known patterns.
These are separate examples: remove line 5; remove lines 3 through 5 inclusively; and remove line 5 through the final input line. The dollar sign is the final-line address. Single quotes prevent the shell from interpreting it.
Use increasing numeric ranges when describing a span. A reversed range does not mean “walk backward”; sed processes its input forward. With multiple input files, ordinary sed processing generally counts across the combined input unless an implementation-specific option requests separate processing.
Read the regular expression
| Pattern | Meaning in these examples |
|---|---|
| ^M | M at the beginning of the line |
| 8$ | 8 at the end of the line |
| . | One character |
| [abc] | One of the listed characters |
| [^abc] | One character other than those listed |
| ^$ | An empty line |
sed '/^$/d' notes.txtThis removes empty lines, not lines containing spaces or tabs. Use /^[[:space:]]*$/ when whitespace-only lines should also be selected in this line-oriented example. The * operator permits zero repetitions, not just one or more.
Slashes commonly delimit a regex address; they are not a universal rule for all sed syntax. Numeric addresses have no slashes, and substitution can use an alternate delimiter. Test a pattern with sed -n and p when you want to preview the lines it selects before deleting them from a generated output.