How to delete lines from a file using the 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 the following purpose

  • To delete or remove specific lines which matches with given pattern.
  • To remove a particular line from the file.
  • To delete expressions as well from a file, which can be identified by a specifying delimiter (such as a comma, tab, or space).

There are fifteen examples listed in this article, which helps you to become a master in sed command.

If you understand and remember all these commands this can be useful in many ways. Also, it saves lot of time when you have some requirements to perform sed command.

Also, you can use the sed command to replace a matching string in a file, and to do so, go to the following article.

Note: Since it’s demonstration article, we use sed command without -i option which prints the contents of the file on Linux terminal by removing the lines.

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 test this, i have created the sed-demo.txt file and added the following contents with line number 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

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

Here N denotes Nth line in a file and d denotes delete a line.

The below sed command removes the first line in 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.

The $ denotes the last line of a file.

The below sed command removes the last line in 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

3) How to Delete Particular Line from a File?

Similar to first command here we delete the  third line in sed-demo.txt file by mentioning the line number 3 as part of the command.

# 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) How to Delete Range of Lines from a File?

The below sed command removes the 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 Delete Multiple Lines from a File?

The sed command is capable of removing 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

5a) How to Delete Lines Other Than the Specified Range from a File?

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

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

After deletion:

3 RHEL
4 Red Hat
5 Fedora
6 Arch Linux

6) How to Delete Empty or Blank Lines from a File?

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

7) How to Delete Lines That Contain a Pattern from a File?

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) How to delete the lines that Containing One of the  Multiple Strings mentioned , from the file?

The following sed command removes the lines in sed-demo.txt file which match the System or Linux pattern.

# 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) How to Delete Lines That Begin with Specific Character from a File?

The following sed command will remove all the lines that start with given character.

To test 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 all the lines that start with 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

10) How to Delete Lines That End with Specified Character from a File?

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 end 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) How to Delete All Lines That Start with Uppercase letter

Use the following sed command to remove all the lines that start with Uppercase.

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

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

12) How to Delete a Matching Pattern Lines with Specified Range in a File?

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

13) How to Delete Pattern Matching Line and also the Next Line?

Use the following sed command to delete the line which containing the pattern ‘System’ and the next line in the file.

# 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

14) How to Delete lines that contains Digits from a File?

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) How to Delete 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

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...