Advanced Bash Scripting
sed
Delete
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
- Overview of the
d
Command - Non-Destructive Deletion
- Deleting a Specific Line
- Deleting a Range of Lines
- In-Place Deletion with
-i
- Quick Reference
- Links & References
Overview of the d
Command
The simplest way to remove lines in sed
is with the delete script d
:
sed 'd' employees.txt
This command reads every line and deletes it, producing no output.
Under the hood, sed
follows this syntax:
sed [OPTIONS] SCRIPT [INPUT-FILE...]
- SCRIPT: A quoted set of editing commands (here,
'd'
). - INPUT-FILE: One or more files to process (defaults to standard input).
Note
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
:
sed '2d' employees.txt
Output:
1|Kriti|Shreshtha|Finance|Financial Analyst|[email protected]|60000
3|Debbie|Miller|IT|Software Developer|[email protected]|80000
4|Enrique|Rivera|Marketing|Marketing Specialist|[email protected]|65000
5|Feng|Lin|Sales|Sales Manager|[email protected]|90000
6|Andy|Luscomb|IT|IT Manager|[email protected]|95000
7|Mark|Crocker|HR|HR Manager|[email protected]|85000
8|Jing|Ma|Engineering|Engineering Manager|[email protected]|100000
Your source file remains intact:
cat employees.txt
Deleting a Specific Line
To drop only the sixth line:
sed '6d' employees.txt
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:
sed '3,5d' employees.txt
This deletes lines 3 through 5.
Warning
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:
# Inspect original
cat employees.txt
# Delete lines 2 through 7 and save changes
sed -i '2,7d' employees.txt
# Verify result
cat employees.txt
Resulting file:
1|Kriti|Shreshtha|Finance|Financial Analyst|[email protected]|60000
8|Jing|Ma|Engineering|Engineering Manager|[email protected]|100000
Warning
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
Command Syntax | Effect | Example |
---|---|---|
sed 'd' | Delete all lines | sed 'd' employees.txt |
sed 'Nd' | Delete Nth line | sed '6d' employees.txt |
sed 'M,Nd' | Delete line range | sed '3,5d' employees.txt |
sed -i 'M,Nd' | In-place deletion | sed -i '2,7d' employees.txt |
Links & References
By mastering these delete operations, you can efficiently cleanse, filter, or reorganize textual data in scripts and pipelines. Happy editing!
Watch Video
Watch video content