copied to clipboard!
string sed

Mastering sed Line Deletion: A Beginner’s Guide from Basics to Beyond

updated: 2026/05/15 created: 2026/04/15

Introduction

The sed command is widely used in daily development and operations as a tool to streamline text processing. Deleting lines, in particular, is a frequent task in log formatting and data preprocessing. However, for beginners, the syntax and behavior can often be confusing.

In this article, we will provide a thorough explanation of how to delete lines with sed using simple examples, while also highlighting common pitfalls.

Reference: FreeBSD Manual Pages - sed

How the Command Works: Deleting Specific Lines

The command we are using this time is:

sed '2d' input.txt

This command means "delete the second line." In sed, you can delete a specific line by using the format Line Number + d.

ElementMeaning
2Target line number
dThe delete command

In other words, only the second line will be excluded from the output.

State Before Execution

First, let's create a target file.

cat << 'EOF' > input.txt hello sed # comment command world EOF

The contents of input.txt are as follows:

hello
sed

# comment
command
world

State After Execution

Now, run the command:

sed '2d' input.txt

The output is:

hello

# comment
command
world

As you can see, "sed," which was on the second line of input.txt, has been deleted.

Execution image

An example of deleting the second line using the sed command. The left side shows the content of input.txt before execution, and the right side shows the output after execution, where the second line has been deleted.

Differences Between GNU sed and BSD sed

There are several differences between BSD sed (commonly used on macOS) and GNU sed (common on Linux). In-place editing is a particularly important point to note.

ItemBSD sedGNU sed
-i OptionExtension is mandatoryExtension is optional
Examplesed -i '' '2d' filesed -i '2d' file

In BSD sed, you must specify an empty string ('') if you do not want a backup extension.

Deleting Multiple Lines

To delete a range of lines, use range specification.

sed '2,3d' input.txt
SyntaxMeaning
2,3From line 2 to line 3
dDelete the specified range

This deletes lines 2 and 3 together.

Deleting Lines by Search Pattern

Here is how to delete lines containing a specific string:

sed '/world/d' input.txt
SyntaxMeaning
/world/Lines containing "world"
dDelete matching lines

Deleting Lines Other Than the Search Pattern

Use this when you want to keep only specific lines.

sed '/world/!d' input.txt
SyntaxMeaning
/world/Lines containing "world"
!Negate the condition
dDelete everything else

Deleting Comment Lines

To delete comment lines (lines starting with #), use the following:

sed '/^#/d' input.txt
Syntax意味
^#Lines starting with #
dDelete matching lines

Deleting Empty Lines

Here is how to delete blank lines:

sed '/^$/d' input.txt
SyntaxMeaning
^$Empty lines
dDelete matching lines

Writing Directly to a File

To actually overwrite the file, use the -i option:

sed -i '' '2d' input.txt
SyntaxMeaning
-i ''Edit file in-place (BSD format)
2dDelete the second line

Quick Reference (Use Cases)

Example 1: Delete the 3rd line

Example 1: Delete the 3rd line

cat << 'EOF' > sample.txt a b c d EOF
sed '3d' sample.txt

Example 2: Delete lines containing "error"

cat << 'EOF' > sample.txt ok error ok EOF
sed '/error/d' sample.txt

Example 3: Delete only empty lines

cat << 'EOF' > sample.txt a b EOF
sed '/^$/d' sample.txt

Common Pitfalls

Misconfiguring sed options can lead to unintended results or errors.

Example:

sed -i '2d' input.txt

This will result in an error in BSD sed because it expects an extension argument after -i.

Deletion Errors Due to Regex Mistakes

Consider the following case:

sed '/./d' input.txt

This will delete every line except completely empty ones. This is because the dot (.) represents "any single character."

Unexpected Deletion Due to Range Misunderstanding

sed '2,$d' input.txt

This deletes everything from the second line to the end of the file. The $ symbol represents the last line.

Summary: Key Points for Deleting Lines with sed

sed is a simple yet extremely powerful command. By mastering the use of line numbers versus search patterns, you can handle a wide variety of text processing tasks.

For beginners, focusing on these points will deepen your understanding:

  • Understand the difference between line number specification and pattern specification.
  • Be mindful of the differences between BSD sed and GNU sed.
  • Practice by creating actual files and testing the commands.

With these points in mind, you will gain confidence in using sed effectively.

Leave a Reply

Your email address will not be published. Required fields are marked *

©︎ 2025-2026 running terminal commands