Mint

Usage of “sed” Command to Delete a Single Line and Multiple Lines in Linux Mint 20

Introduction:

The “sed” command in Linux is a multi-purpose command that is used to substitute, find, replace, insert, delete, and search text in files. “sed”, in fact, stands for Stream Editor. In this article, we intend to explore the usage of this command for deleting a single line as well as multiple lines from a file in Linux Mint 20.

However, before proceeding with these methods, we would like to share with you the general syntax of this command which is as follows:

$ sed [option] [filename]

Also, for all the methods discussed below, we will be using the same sample text file named List.txt. The contents of our sample file are shown below:

Methods of Deleting a Single Line using the “sed” Command in Linux Mint 20:

If you want to delete a single line from a file using the “sed” command, then depending on its position, you can choose one of the following two methods:

Method # 1: Deleting any Single Line other than the Last One:

For deleting any line from your file except for the last one, you will have to run the “sed” command in the manner shown below:

$ sed ‘NUMd’ filename

Here, you need to replace NUM with the line number of the line that you want to delete for example ‘1d’ for the first line of the file followed by the exact name of that file.

You can see from the following image that the first line of our original text file has been deleted.

Method # 2: Deleting the Last Line:

Now, if you want to delete the last line of your file, then you will have to run the “sed” command in the manner shown below:

$ sed ‘$d’ filename

Here, the ‘$’ symbol corresponds to the last line of the specified file.

You can see from the output shown in the following image that the last line of our original text file has been deleted.

Methods of Deleting Multiple Lines using the “sed” Command in Linux Mint 20:

If you want to delete multiple lines from a file using the “sed” command, then depending on your exact need, you can choose one of the following three methods:

Method # 1: Deleting a Range of Lines:

For deleting a specified range of lines, you will have to execute the “sed” command in the manner shown below:

$ sed ‘NUM, NUM2d’ filename

Here, NUM represents the starting line number for deletion whereas NUM2 represents the ending line number for deletion. We wanted to delete the lines 1-3 from our specified file so we replaced NUM with 1 and NUM2 with 3.

You can see from the output shown in the following image that the lines 1-3 have been successfully deleted from our file.

Method # 2: Deleting Some Specific Lines:

If you do not want to delete an entire range of lines rather you want to pick some lines at random for deletion, then you will have to execute the “sed” command in the manner shown below:

$ sed ‘NUMd;NUM2d; NUM3d;….’ filename

Here, NUM, NUM2, and NUM3 refer to the line numbers of all the different lines that you want to delete.

You can see from the following image that our specified lines have been successfully deleted.

Method # 3: Deleting all the Lines except for the Specified Range:

Finally, if you want to delete all the lines from a file except for a specified range, then you will have to execute the “sed” command in the manner shown below:

$ sed ‘NUM, NUM2!d’ filename

Here, NUM represents the starting line number and NUM2 represents the ending line number of the range of lines that you want to prevent from deletion.

You can see from the following image that all other lines except for the specified range have been successfully deleted from our file.

Conclusion:

This article explained to you one of the many uses of the “sed” command in Linux Mint 20. By using this command in the ways described in this article, you will be able to delete lines from any file on Linux very conveniently.

Similar Posts