Filter Lines with Sed
Learn how to filter lines with sed using line numbers, patterns, ranges, and negation, with practical command examples and limitations.
What filtering lines with sed means
Filtering lines with sed means selecting only the input lines that satisfy a condition and excluding the rest. In sed terms, this is usually done by deleting unwanted lines with the d command or printing selected lines with the p command. The result is a reduced stream of text rather than a replacement of content within each line.
The clearest form is to suppress automatic output with the -n option and then print only matching lines. This turns sed into a line filter. Without -n, sed prints every input line by default, so p would duplicate selected lines instead of filtering them.
Filtering by line number
Line numbers are expressed as addresses before the p command. A single address selects one line, while a comma-separated pair selects an inclusive range. The dollar sign represents the last line.
sed -n '5p' file.txt
sed -n '10,20p' file.txt
sed -n '15,$p' file.txt
The first command prints line 5, the second prints lines 10 through 20, and the third prints from line 15 to the end of the file. Lines outside those addresses are omitted from the output.
Filtering by pattern
A regular expression address selects every line containing a match. When combined with -n and p, only the matching lines remain.
sed -n '/error/p' app.log
sed -n '/^[[:space:]]*$/d' file.txt
The first example prints lines containing the literal text error. The second example deletes blank or whitespace-only lines while leaving all other lines intact, which is another valid filtering approach for removing unwanted content.
Filtering a range between patterns
A range can be bounded by two regular expressions. The range starts when the first pattern matches and continues through the line where the second pattern matches. This is useful for extracting blocks such as configuration sections or log entries.
sed -n '/^START/,/^END/p' data.txt
This prints every line from one beginning with START through the next line beginning with END, inclusive. If the start pattern appears again before an end pattern, each start restarts the range.
Inverting the filter
The exclamation mark after an address inverts the selection. This is a concise way to remove a line or range instead of selecting it.
sed '/^#/d' config.conf
sed '/TODO/!d' notes.txt
The first command deletes comment lines that begin with #, effectively filtering them out. The second deletes every line that does not contain TODO, leaving only the matching lines. Both forms exclude content according to a condition rather than altering the retained lines.
Deleting versus printing
There are two equivalent ways to express a filter: retain the wanted lines with print, or remove the unwanted lines with delete. The -n and p form is explicit about what is kept, while the d form is explicit about what is discarded. Either approach can produce the same output.
sed -n '/keep/p' file.txt
sed '/keep/!d' file.txt
Both commands output only lines containing keep. The choice is a matter of readability and which condition is easier to describe.
Practical examples
sed -n '1,10p' access.log
sed -n '/Failed password/p' auth.log
sed '/^[[:space:]]*$/d' input.txt
sed -n '/^2026-01-15/,/^2026-01-16/p' events.log
These examples show the common uses: keeping a numbered header block, extracting matching log entries, removing empty lines, and isolating a date-bounded range. In each case, the output is a subset of the original input.
Limits to keep in mind
Sed filters one line at a time and does not provide built-in context retention such as the lines before or after a match. It is also line-oriented, so filtering content that spans variable line boundaries can be cumbersome. When the goal is to select lines with surrounding context, another tool may be more suitable.
Documentation in this response is based on standard sed addressing and print/delete behavior. Exact support for regular expressions and address syntax can vary between sed implementations, so commands should be tested against the intended environment before use in production workflows.