How to delete lines from a file using sed command

sed command stands for Stream Editor. It is used to perform basic text transformations in Linux.

sed is one of the important command, which plays major role in file manipulations. It can be used for many purposes some of which are listed below:

  • To delete or remove specific lines which matches with given pattern.
  • To remove a particular line based on position in a file.
  • Removing lines using regular expressions.

There are 27 examples listed in this article, which will help you to become a master in sed command.

If you could memorize these commands, it will save a lot of your time when you have the requirement to perform various admin tasks in your projects.

Also, check this article on how you can use the sed command to replace a matching string in a file.

Note: Since this is a demonstration article, we use sed command without the -i option, which is similar to the “dry run” option, and will display the actual output without making any changes in the file.

But, if you would like to remove the lines from the source file in real environment then use the -i option with sed command.

To demonstrate sed command, we have created the ‘sed-demo.txt’ file and added the following contents with line numbers for better understanding.

# cat sed-demo.txt

1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

Part-I) Removing lines based on a position in the file

The first part shows how to use sed command to delete lines based on a position in the file.

1) How to delete first line from a file?

To delete the first line from a file, use the following syntax:

Syntax:

sed 'Nd' file

Details;

  • N – Denotes ‘Nth’ line in a file
  • d – Indicates the deletion of a line.

The below sed command removes the first line from the ‘sed-demo.txt’ file:

# sed '1d' sed-demo.txt

After deletion:
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

2) How to delete last line from a file?

If you would like to delete the last line from a file, use the following syntax ($ denotes the last line of a file).

The below sed command removes the last line from the ‘sed-demo.txt’ file:

# sed '$d' sed-demo.txt

After deletion:
1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu

The following sed command removes the first and last line from the file:

# sed '1d;$d' sed-demo.txt

After deletion:
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu

3) Deleting a particular line from a file

In this example, we will delete the third line from the ‘sed-demo.txt’ file using sed command as shown below. Similarly, any line can be removed by entering the line number instead of the number 3.

# sed '3d' sed-demo.txt

After deletion:

1 Linux Operating System
2 Unix Operating System
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

4) Deleting range of lines

The sed command removes any range of given lines from a file. We need to enter the ‘minimum’ and ‘maximum’ line numbers. The below example removes lines ranging from 5 to 7.

# sed '5,7d' sed-demo.txt

After deletion:
1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
8 Debian
9 Ubuntu
10 openSUSE

5) How to remove multiple lines

The sed command is capable of removing a set of given lines.

In this example, the following sed command will remove 1st , 5th , 9th , and the last line.

# sed '1d;5d;9d;$d' sed-demo.txt

After deletion:

2 Unix Operating System
3 RHEL
4 Red Hat
6 Arch Linux
7 CentOS
8 Debian

5.a) Deleting lines other than the specified range

Use the following sed command to remove all the lines from the file, except the specified range of lines:

# sed '3,6!d' sed-demo.txt

After deletion:

3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux

Details;

  • ! – Negation operator is used to keep the specified lines

Use the below command to delete all lines other than the first line:

# sed '1!d' sed-demo.txt

After deletion:

1 Linux Operating System

Use the below command to delete all lines except the last line:

# sed '$!d' sed-demo.txt

After deletion:

10 openSUSE

6) Deleting empty or blank lines

The following sed command will  remove the empty or blank lines from ‘sed-demo.txt’ file.

# sed '/^$/d' sed-demo.txt

After deletion:

1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

Part-II) Removing lines based on pattern match

The second part shows, how to use sed command to remove lines in a file that match a given pattern.

7) Removing lines that contain a pattern

The following sed command will remove the lines which match the System pattern in ‘sed-demo.txt’ file .

# sed '/System/d' sed-demo.txt

After deletion:

3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

8) Deleting lines that contain one of several strings

The following sed command removes the lines which match with the System or Linux pattern from the file ‘sed-demo.txt’:

# sed '/System\|Linux/d' sed-demo.txt

After deletion:

3 RHEL
4 Red Hat
5 Fedora
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

9) Deleting lines that begin with specific character

The following sed command will remove all the lines that begin with a given character. To demonstrate this, I have created another file called ‘sed-demo-1.txt’ with following contents:

# cat sed-demo-1.txt

After deletion:
Linux Operating System
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu
Arch Linux - 1
2 - Manjaro
3 4 5 6

The following sed command removes all the lines that start with character R :

# sed '/^R/d' sed-demo-1.txt
 
After deletion:
Linux Operating System
Unix Operating System
Fedora
debian
ubuntu
Arch Linux - 1
2 - Manjaro
3 4 5 6

The following sed command will remove the lines that begin with the character either R or F :

# sed '/^[RF]/d' sed-demo-1.txt

After deletion:
Linux Operating System
Unix Operating System
debian
ubuntu
Arch Linux - 1
2 - Manjaro
3 4 5 6

The following sed command will remove all the lines that starts with character L and ends with System string :

# sed '/^(L).*(System)/d' sed-demo-1.txt

After deletion:
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu
Arch Linux - 1
2 - Manjaro
3 4 5 6

10) Removing lines that end with specified character

The following sed command removes all the lines that end with character m :

# sed '/m$/d' sed-demo.txt

After deletion:
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

The following sed command removes all the lines that ends with character either x or m :

# sed '/[xm]$/d' sed-demo.txt

After deletion:

3 RHEL
4 Red Hat
5 Fedora
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

11) Deleting all lines that start with uppercase

Use the following sed command to remove all the lines that start with an Uppercase letter:

# sed '/^[A-Z]/d' sed-demo-1.txt

After deletion:
debian
ubuntu
2 - Manjaro
3 4 5 6

12) Deleting a matching pattern lines with specified range

The below sed command removes the pattern Linux only if it is present in the lines from 1 to 6 :

# sed '1,6{/Linux/d;}' sed-demo.txt

After deletion:
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

12.a) Deleting only the last line if it contains a given pattern

The below sed command only removes the last line if it contains the pattern “openSUSE

# sed '${/openSUSE/d;}' sed-demo.txt

After deletion:
1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu

13) How to delete pattern matching lines and also the next Line?

Use the following sed command to delete the line which matches the pattern System and the subsequent line in the file as well.

# sed '/System/{N;d;}' sed-demo.txt

After deletion:
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux
7 CentOS
8 Debian
9 Ubuntu
10 openSUSE

13.a) Deleting lines starting from a pattern till the last line

The below sed command removes the line that matches the pattern “CentOS”, and also deletes all the subsequent lines till the end of the file:

# sed '/Centos/,$d' sed-demo.txt

After deletion:
1 Linux Operating System
2 Unix Operating System
3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux

14) Deleting lines that contains Digits/Numbers

The below sed command removes all the lines that contains ‘digits’:

# sed '/[0-9]/d' sed-demo-1.txt

After deletion:

Linux Operating System
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu

The below sed command removes all the lines which only begins with digits:

# sed '/^[0-9]/d' sed-demo-1.txt

After deletion:

Linux Operating System
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu
Arch Linux - 1

The below sed command removes all the lines which ends with digits:

# sed '/[0-9]$/d' sed-demo-1.txt

After deletion:

Linux Operating System
Unix Operating System
RHEL
Red Hat
Fedora
debian
ubuntu
2 - Manjaro

15) Deleting lines that contains Alphabetic Characters from a file

The below sed command removes all the lines that contains any alphabetic characters.

# sed '/[A-Za-z]/d' sed-demo-1.txt

After deletion:
3 4 5 6

Closing Notes

In this guide, we have shown you several examples to delete lines from a file using the sed command.

If you have any questions or feedback, feel free to comment below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

7 Comments on “How to delete lines from a file using sed command”

  1. Dear Magesh Maruthamuthu,

    Ver nice article, with well-stated examples and good, succinct explanations.

    I came here looking for #13, how to delete a line with a matching pattern and the next line. In my case I needed to delete the next TWO lines. The parenthetical syntax threw me.

    Here is what I came up with to solve that problem. First I’ll show your example file:

    # cat sed-demo.txt

    1 Linux Operating System
    2 Unix Operating System
    3 RHEL
    4 Red Hat
    5 Fedora
    6 Arch Linux
    7 CentOS
    8 Debian
    9 Ubuntu
    10 openSUSE

    This will delete the first two lines as you showed:
    # sed ‘/System/{N;d;}’ sed-demo.txt

    I experimented a bit, and this is what I initially came up with to delete the next two lines:
    # sed ‘/Arch/{N;N;d;}’ sed-demo.txt

    After deletion:

    1 Linux Operating System
    2 Unix Operating System
    3 RHEL
    4 Red Hat
    5 Fedora
    9 Ubuntu
    10 openSUSE

    To delete a line with a given pattern, and then 99 additional lines after it….. would be cumbersome; and what about ‘programming’ that, by substituting ‘N’ with a variable. Anyway, I find having to supply 99 sets of ‘N;’ in a row to be just bad form.

    So, I was wondering if there was a form of shorthand to simplify ‘{N;N;d}’; and I found this to work (that is a ‘comma’ before the ‘plus sign’):
    # sed ‘/Arch/,+2d’ sed-demo.txt

    That shorthand does lend itself to replacing the ‘+2′ with a variable.
    So, this would work to remove the line with a matching pattern and the next five lines after it (note the replacment of single quotes with double quotes):
    # x=’5’; sed “/Unix/,+${x}d” sed-demo.txt

    After deletion:

    1 Linux Operating System
    7 CentOS
    8 Debian
    9 Ubuntu
    10 openSUSE

    I hope what I’ve shared here is useful to many others.

    I had done quite a bit of google searching, and man/info page reading attempting to elegantly solve this problem.
    I found this: https://unix.stackexchange.com/questions/29906/delete-range-of-lines-above-pattern-with-sed-or-awk
    One would have to read the article, yet, the ‘comment’ by ‘Akaks’ on Jan 26th 2015 shows the only other example for solving this like I laid out above.

    Thank you.
    -Joe Wulf

  2. i let the same question on -> https://stackoverflow.com/questions/65315765/how-i-should-use-sed-for-delete-specific-strings-and-allow-duplicate-with-more-c

    Hi there if possible use sed for delete specific strings and dont delete the duplicate ones with more characters ?

    for example if i had in a huge text file the lines (really had 17417 lines) :
    ./usr
    ./usr/share
    ./usr/share/mime-info
    ./usr/share/mime-info/libreoffice7.0.mime
    ./usr/share/mime-info/libreoffice7.0.keys
    ./usr/share/appdata
    ./usr/share/appdata/libreoffice7.0-writer.appdata.xml
    ./usr/share/appdata/org.libreoffice7.0.kde.metainfo.xml
    ./usr/share/applications
    ./usr/share/applications/libreoffice7.0-xsltfilter.desktop
    ./usr/share/icons
    ./usr/share/icons/gnome
    ./usr/share/icons/gnome/16×16
    ./usr/share/icons/gnome/16×16/mimetypes
    ./usr/share/icons/gnome/16×16/mimetypes/libreoffice7.0-oasis-formula.png

    i just want to delete the lines like :
    ./usr
    ./usr/share
    ./usr/share/mime-info
    ./usr/share/appdata
    ./usr/share/applications
    ./usr/share/icons
    ./usr/share/icons/gnome
    ./usr/share/icons/gnome/16×16
    ./usr/share/icons/gnome/16×16/mimetypes

    and just result in a new file with the full path deleting the “.” at the start.
    Someone had ideas how i can do that ?

Leave a Reply to Yang Cancel reply

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