×


How to use "sed" Command to Delete a Single Line and Multiple Lines in Linux Mint 20 ?

The "sed" command in Linux is a multi-purpose command that is used to substitute, find, replace, insert, delete, and search text in files. With SED, we can edit complete files without actually having to open it. Sed also supports the use of regular expressions, which makes sed an even more powerful test manipulation tool. "sed", in fact, stands for Stream Editor.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux System commands queries.

In this context, we shall look into how to use "sed" command for deleting a single line as well as multiple lines from a file in Linux Mint 20.



"sed" command syntax

The general syntax of "sed" commandis given below for your information:

$ sed [option] [filename]


Ways 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:


1. Deleting any Single Line other than the Last One

to delete any line from your file except for the last one, you will have to run the "sed" command as 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 will see from the output that the first line of our original text file has been deleted.


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 will see from the output that the last line of our original text file has been deleted.


Ways to delete 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.


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.

The output will show that that the lines 1-3 have been successfully deleted from our file..


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.


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.


[Need help to fix all your Linux System issues? We can help you. ]


Conclusion

This article covers the different ways you can use the "sed" command in Linux Mint 20. Sed command or Stream Editor is very powerful utility offered by Linux/Unix systems. It is mainly used for text substitution , find & replace but it can also perform other text manipulations like insertion, deletion, search etc.