Skip to main content
Stream Editor (sed) is a powerful command-line utility for transforming text in a pipeline. In this guide, we’ll explore how to delete lines from input files using the d command, covering non-destructive edits, specific-line removals, range deletions, and in-place updates.

Table of Contents

  1. Overview of the d Command
  2. Non-Destructive Deletion
  3. Deleting a Specific Line
  4. Deleting a Range of Lines
  5. In-Place Deletion with -i
  6. Quick Reference
  7. Links & References

Overview of the d Command

The simplest way to remove lines in sed is with the delete script d:
This command reads every line and deletes it, producing no output. Under the hood, sed follows this syntax:
  • SCRIPT: A quoted set of editing commands (here, 'd').
  • INPUT-FILE: One or more files to process (defaults to standard input).
Wrap your script in single quotes (e.g., 'd') so the shell interprets it literally.

Non-Destructive Deletion

By default, sed writes the transformed text to standard output and leaves the original file unchanged. To delete line 2 from employees.txt:
Output:
Your source file remains intact:

Deleting a Specific Line

To drop only the sixth line:
This command filters out line 6 from the output stream, leaving all others.

Deleting a Range of Lines

Use a comma-separated address pair to remove a block of lines:
This deletes lines 3 through 5.
Address ranges must ascend (e.g., 3,5d). Specifying 5,3d is invalid and will have no effect.

In-Place Deletion with -i

To modify the file directly, add the -i (in-place) option:
Resulting file:
On macOS, sed -i requires a zero-length extension: sed -i '' '2,7d' file.txt. Always back up critical data before in-place edits.

Quick Reference


By mastering these delete operations, you can efficiently cleanse, filter, or reorganize textual data in scripts and pipelines. Happy editing!

Watch Video