sed Delete Lines: Remove Lines by Number, Pattern, or Range
When working with text files, you will often need to remove specific lines, whether it is stripping comments from a configuration file, cleaning up blank lines in log output, or cutting a range of lines from a data dump. The sed stream editor handles all of these cases from the command line, without opening the file in an editor.
sed processes input line by line, applies your commands, and writes the result to standard output. The original file stays untouched unless you explicitly tell sed to edit in place. If you need to find and replace text rather than remove entire lines, see How to Use sed to Find and Replace Strings in Files
.
This guide explains how to use the sed delete command (d) with practical examples covering line numbers, patterns, ranges, and regular expressions.
How the Delete Command Works
The general form of a sed delete expression is:
sed 'ADDRESSd' fileADDRESS selects which lines to delete, and d is the delete command. When sed encounters a line that matches the address, it removes that line from the output entirely. Any line that does not match the address is printed as usual.
For demonstration purposes, the examples in this guide use the following file:
red
green
blue
yellow
white
black
orange
purpleDelete by Line Number
The simplest form of delete targets a specific line number. To remove the third line, place 3 before the d command:
sed '3d' colors.txtred
green
yellow
white
black
orange
purple
Notice that “blue” (which was on line 3) is gone from the output, but the original colors.txt file is unchanged. This is the default behavior of sed, and it gives you a safe way to preview changes before committing them.
Sometimes you need to remove the last line of a file without knowing how many lines it contains. The $ address represents the last line:
sed '$d' colors.txtred
green
blue
yellow
white
black
orange
Here “purple” (the last line) has been removed from the output.
Delete Multiple Specific Lines
If you need to remove several individual lines, separate the addresses with a semicolon. The following command deletes lines 2, 5, and 8 in a single pass:
sed '2d;5d;8d' colors.txtred
blue
yellow
black
orange
The output is missing “green” (line 2), “white” (line 5), and “purple” (line 8). You can list as many addresses as needed this way.
Delete a Range of Lines
To delete a block of consecutive lines, specify a range with two numbers separated by a comma. The following command removes lines 3 through 6:
sed '3,6d' colors.txtred
green
orange
purple
Everything from “blue” (line 3) through “black” (line 6) is gone, and the remaining lines print normally.
You can also combine a line number with $ to delete from a given line to the end of the file. This keeps only the first four lines:
sed '5,$d' colors.txtred
green
blue
yellow
Adding ! after the address inverts the selection, so sed deletes every line that is not in the range. The following command keeps only lines 3 through 5 and removes everything else:
sed '3,5!d' colors.txtblue
yellow
white
This is a convenient way to extract a slice from a file without piping through head and tail.
Delete Every Nth Line
GNU sed supports the step address first~step, which matches every Nth line starting from a given position. To delete every even-numbered line (2, 4, 6, …):
sed '0~2d' colors.txtred
blue
white
orange
The first number is the starting offset (0 means “before the first line”, so the first match is line 2) and the second number is the step. In this case sed deletes lines 2, 4, 6, and 8.
To delete every third line starting from line 3:
sed '3~3d' colors.txtred
green
yellow
white
orange
purple
Lines 3 (“blue”) and 6 (“black”) are removed. This syntax is specific to GNU sed and is not available in the BSD version shipped with macOS.
Delete Lines Matching a Pattern
One of the most common uses of sed delete is removing lines that contain a specific string. Wrap the search pattern in forward slashes before the d command:
sed '/blue/d' colors.txtred
green
yellow
white
black
orange
purple
Every line containing the string “blue” is removed from the output. Keep in mind that this is a substring match, so a pattern like /bl/d would also delete the line “black” because it contains “bl”.
If you want the match to be case-insensitive, add the I flag after d:
sed '/Blue/Id' colors.txtThis removes lines containing “blue”, “Blue”, “BLUE”, or any other case variation.
Delete Lines Not Matching a Pattern
Adding ! after the pattern address inverts the match, so sed deletes every line that does not contain the pattern. The following command removes all lines that do not have the letter “e”:
sed '/e/!d' colors.txtred
green
blue
yellow
white
orange
purple
Only “black” (line 6) was removed because it is the only line without an “e”. This works similarly to grep
, but the advantage of sed is that you can combine it with other editing commands in the same expression.
Delete a Range Between Two Patterns
You can use two patterns separated by a comma to define a range. sed starts deleting at the first line that matches the opening pattern and stops after the first line that matches the closing pattern. The following command removes everything from “green” through “white”:
sed '/green/,/white/d' colors.txtred
black
orange
purple
Both the “green” and “white” lines are included in the deletion, along with “blue” and “yellow” between them.
You can also mix a line number with a pattern. This deletes from line 1 through the first line that contains “blue”:
sed '1,/blue/d' colors.txtyellow
white
black
orange
purple
This kind of mixed address is useful when you know where the range starts (a fixed line) but not where it ends.
Delete Empty Lines
Removing blank lines is one of the most common sed tasks. To delete completely empty lines, use the pattern ^$, which matches lines where the beginning and end have nothing between them:
sed '/^$/d' file.txtThis works well for truly empty lines, but it will not catch lines that contain only spaces or tabs. To remove those as well, use the [[:space:]] character class:
sed '/^[[:space:]]*$/d' file.txtThe * quantifier matches zero or more whitespace characters, so this pattern covers both empty lines and lines that appear blank but contain invisible whitespace.
Delete Lines Matching a Regular Expression
The pattern between the slashes is a regular expression, so you can use anchors, character classes, and quantifiers for more precise matching.
A common example is stripping comment lines from a configuration file. The ^# pattern matches any line that starts with #:
sed '/^#/d' config.txtTo delete lines that end with a semicolon, anchor the pattern to the end of the line with $:
sed '/;$/d' source.txtYou can also match by line length. The following command deletes lines that are shorter than 5 characters. The \{0,4\} quantifier means “between 0 and 4 of any character”:
sed '/^.\{0,4\}$/d' file.txtIf you find the backslash escaping awkward, use extended regular expressions with the -E flag (or -r on older systems). This lets you write the same expression without escaping the braces:
sed -E '/^.{0,4}$/d' file.txtCombining Delete with Other Commands
One of the strengths of sed is that you can chain multiple operations in a single expression. For example, the following command first removes comment lines and then replaces “localhost” with “127.0.0.1” in whatever remains:
sed '/^#/d; s/localhost/127.0.0.1/g' config.txtThe commands are separated by a semicolon and execute left to right. If a line is deleted by an earlier command, the later commands never see it, so the replacement only applies to non-comment lines.
Editing Files In Place
All of the examples above print the modified text to standard output without changing the original file. When you are satisfied with the result, add the -i flag to apply the changes directly:
sed -i '/^$/d' file.txt-i flag modifies the file permanently. Always preview the output without -i first, or create a backup by passing a suffix: sed -i.bak '/^$/d' file.txt. This saves the original as file.txt.bak.On macOS, the BSD version of sed requires an explicit extension argument even if you do not want a backup. Use sed -i '' '/^$/d' file.txt for in-place editing without creating a backup file.
Quick Reference
For a printable quick reference, see the sed cheatsheet .
| Expression | What it deletes |
|---|---|
sed '3d' |
Line 3 |
sed '$d' |
Last line |
sed '2d;5d' |
Lines 2 and 5 |
sed '3,6d' |
Lines 3 through 6 |
sed '5,$d' |
Line 5 to end of file |
sed '3,5!d' |
All lines except 3 through 5 |
sed '0~2d' |
Every even-numbered line |
sed '/pattern/d' |
Lines matching pattern |
sed '/pattern/!d' |
Lines not matching pattern |
sed '/start/,/end/d' |
From first match of start to first match of end |
sed '/^$/d' |
Empty lines |
sed '/^#/d' |
Comment lines starting with # |
FAQ
How do I delete a line containing a specific word?
Use sed '/word/d' file.txt. This removes any line where “word” appears as a substring. If you want to match the whole word only (so that “keyword” is not affected), use word boundaries: sed '/\bword\b/d' file.txt.
Can I delete lines from multiple files at once?
Yes. Pass multiple filenames after the expression: sed -i '/pattern/d' file1.txt file2.txt. To process files recursively, combine sed with find: find . -name "*.log" -exec sed -i '/DEBUG/d' {} +.
What is the difference between sed '/pattern/d' and grep -v 'pattern'?
Both remove matching lines from the output. The difference is that sed can combine deletion with other editing commands in the same expression and supports in-place editing with -i. If all you need is simple line filtering, grep -v is shorter to type.
How do I delete a range of lines and save the result?
You can either redirect the output to a new file (sed '3,6d' file.txt > cleaned.txt) or edit in place with a backup (sed -i.bak '3,6d' file.txt). Do not redirect to the same input file, because the shell will truncate it before sed has a chance to read it.
Conclusion
The sed delete command gives you a fast way to remove lines by number, pattern, or range without opening a file in an editor. For replacing text within lines rather than removing them, see the companion guide on sed find and replace
. If you need to work with individual columns or fields within a line, awk
is often the better tool for the job.
![]()