• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How to Delete Lines with Sed in Linux

Bhagwad Park

Published on: December 27, 2023

Categories: Linux Command Line, VPS Hosting 0

“Sed” stands for “Stream Editor”, and is a Linux tool designed for modifying input streams of text on a line-by-line basis. When compared to other text manipulation tools like awk and grep, sed is used for quick editing functions. Awk, on the other hand, is a full-fledged programming language with control structures and string manipulation. Sed, awk, and grep are all different tools focusing on text. Even though sed was first developed in the 1970s, it’s still incredibly useful even today.

One of the biggest benefits of sed is that it operates on a line-by-line basis, meaning that even arbitrarily large text streams can be processed efficiently, compared to other editors which attempt to load the entire file into memory before beginning processing.

In this article, I’ll show you how to delete lines using sed. The focus will be on deleting specific line numbers, ranges of lines, and multiple standalone lines along with exclusions.

Table of Contents
  • Sed Syntax for Deleting Lines
  • Sed Doesn’t Save Changes to its Output
    • Two Ways of Taking Backups When Deleting Lines with sed
  • Deleting Multiple Lines and Ranges with sed
    • Deleting Multiple Lines at the Same Time
    • Deleting Line Ranges
    • Combining Range Deletions and Single Line Deletions
    • Deleting the Last Line
  • Deleting Lines that Match a Pattern
    • Case Insensitive Matching
    • Matching Patterns at the Beginning of Lines
  • Conclusion

Sed Syntax for Deleting Lines

The syntax of sed looks like this:

sed [OPTIONS] 'COMMAND' file

The “command” is a string that takes the format:

[address]X[options]

The “address” is either the line number of the lines on which you want sed to operate, or a pattern, which we’ll discuss below. “X” refers to what you want to do with the lines once they’ve been matched. In our case, since we want to delete lines, we use the letter “d”. Keep in mind that in sed, the numbering starts from 1 and not zero. So the first line in any document is always “1”.

Putting all this together, here’s a basic syntax for how to delete a single line in sed:

sed 'Nd' file.txt

Here, “N” refers to the line number, and “d”, as mentioned above, is “delete”. Here’s the output of the above command on a text file “example.txt”, in which I’ve numbered the lines and am deleting the 3rd line:

Sed Delete Line
Sed Delete Line

As you can see, the 3rd line is gone.

Sed Doesn’t Save Changes to its Output

An important point to remember is that sed doesn’t automatically save the changes to the text stream back to the file. So in the above example, the file “example.txt” is still unchanged! Sed merely sends its output to the standard output – which is usually the terminal screen. This is called a “non-destructive” operation and is one of the key aspects of sed from a safety perspective.

If you want to save the changes you make to the input file, you must use the “-i” flag like this:

sed -i '3d' example.com

The above command will not only remove the 3rd line from the output but save the changes back to “example.txt”. As you can imagine, this is a dangerous option and should only be exercised carefully.

Two Ways of Taking Backups When Deleting Lines with sed

To avoid the dangers of permanently changing files with sed, there are two ways you can take a backup. The first is by redirecting the output to another file like this:

sed '3d' example.txt > newfile.txt

The above example will save the changes to “newfile.txt” and won’t touch “example.txt”.

The second way of saving a backup is through an extension of the sed “-i” option. For example, the following command:

sed -i.bak '3d' example.txt

This will save the changes to example.txt, but will also save a backup copy of the file called “example.txt.bak”. The syntax for the option is:

-i[SUFFIX]

Here, [suffix] is the extension you want to use for the backup file. It can be anything you want.

The above two techniques help you remain safe while using sed, and allow you to experiment with various removal techniques without any danger of permanent damage.

Deleting Multiple Lines and Ranges with sed

In the above simple example, I showed you how to delete a single line with sed. But you can do a lot more than just remove single lines. In this section, I’ll show you how to delete multiple lines with a single command, delete line ranges, delete lines that match a pattern, and delete lines that don’t match a pattern.

Deleting Multiple Lines at the Same Time

Let’s say you want to remove the first and third lines from a text file. Using sed, you can do this with the following syntax:

sed 'Nd;Md' example.txt

Where “N” and “M” are the line numbers you want to remove. You specify each line you want to remove and separate them with a semicolon (;). You can add as many line numbers as you want.

Deleting Line Ranges

If you want to delete a chunk of text between two lines, then sed allows you to specify the start and end lines using the following syntax:

sed 'N,Md' file.txt

So when you want to delete a range, get the starting and ending line numbers, plug them into the above command, and you’re set!

Combining Range Deletions and Single Line Deletions

Sed even allows you to combine the two. You can delete a range of lines, as well as a bunch of single lines using the same command. For example, the following command:

sed 'N,Md;Od' file.txt

This will delete the range of lines from N to M as mentioned above, but it will also delete the line number represented by “O”. As you can see, sed is very flexible and you can accomplish several tasks using the same command if you know the syntax.

Deleting the Last Line

You can also delete the last line of a file by typing:

sed '$d' example.txt

Deleting Lines that Match a Pattern

Since sed parses input line by line, you can specify a pattern that you want to identify, and then delete those lines. For example, to find a basic pattern, you can use:

sed '/apple/d' example.txt

This will find and delete all the lines in example.txt with the text “apple” in them.

Case Insensitive Matching

By default, sed takes the case of the pattern into account. So “apple” will also match “Apple”. If you want to remove all lines that match a pattern regardless of case, then you can instruct sed to ignore the case by using the following command:

sed '/apple/Id' example.txt

Note the extra “I” before the delete command.

Matching Patterns at the Beginning of Lines

Let’s say you have a code file that you want to streamline for production and remove all comments. Different languages have different syntaxes for comments. Python and Linux files, for example, use the hash (#) character. So if you want to remove all lines in a file that start with “#”, use the following:

sed '/^#/d' example.txt

The symbol denoting the start of the string is the caret (^), followed by “#”. This single command will remove all comments from a Linux script or a Python code file. As you can probably guess, this syntax is part of the regular expression pattern, which means you can use RegEx as well for more sophisticated matching.

Conclusion

There’s a lot more to sed than what I’ve explained above. You can use regular expressions, and even perform complex operations like using RegEx to surround specific strings with parentheses, and more. Don’t forget to redirect the output to another file, or to create a backup by using the “-i[EXTENSION]” parameter for in-place editing. Sed is a powerful tool, but it can cause havoc if you use it carelessly.

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

Docker Build Cache – How it Works and Invalidating it

Docker heavily optimizes its build process with caching to save time. But it's important to know how to invalidate it.

Using Systemctl to Enable Services on Boot on Linux

You can use the systemctl tool to start services automatically on boot. We do this using the "enable" keyword.

Bash Debug Mode: set -x and the PS4 Variable

When coding a program, you usually have a full IDE set up with autocomplete and stack tracing, allowing you to pause execution at any point and see what’s going on under the hood. These tools make coding more productive, and they’ve been in use for a long time. But what if you’re just quickly coding […]

Dealing with Cron Job Logs in Linux

Cron job logs keep track of which cron jobs run, and their details. This helps with troubleshooting and performance management.

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA