The “sed” is a command-line utility in Linux that stands for “stream editor.” It manipulates the text within the files or some input stream in various ways. It performs a sequence (provided by the user) on the input, which is then written on a file or output stream. Although many functions can be done by it, the most common use of the sed command would be to search and replace or delete some text.
This write will pin all the details about the sed command to delete a line in Linux. The content covered in this post is:
- How to Use sed Command to Delete a Line?
- Delete a Single Line
- Delete Multiple Lines
- Delete Lines in a Range From the Text
- Delete All Lines Except Specified Lines (Range)
- Delete Lines based on a Pattern
- Delete Blank Line(s)
Now, let’s start with the basic working of the sed command.
How to Use sed Command to Delete a Line in Linux?
The first step to utilize any Linux command is understanding its syntax, as shown below.
$ sed [option] [filename]
The core part of the syntax is the filename (along with its path if the file resides in other than PWD). The list of options can be viewed using the command:
$ man sed
When the above command is executed, it’ll bring up the following screen in the terminal.
It would be best if you pressed ‘h; to get more information.
To get even more information, keep pressing the ‘Enter’ key until the end of the page is reached, and now we’d explain it using examples.
Before starting, the following text file is used as an experiment in this guide.
$ cat file
There are a few blank lines between Linux Mint and Kubuntu.
How to Delete a Single Line Using sed Command?
You must type the following command to delete a single line from the text using the sed command.
$ sed 'Line-No+d' File-Name
In the above syntax, you need to replace ‘Line-No’ with the line number from which you wish to delete the text, and ‘d’ stands for delete. File-Name is to be changed also. We’d now delete line 2 as an example from ‘file’.
$ sed '1d' file
As seen in the above image, the contents of ‘file’ are read using the cat command, and then we’ve deleted the first line using the sed command. You can replace ‘1’ with any line number you wish to delete.
How to Delete Multiple Lines Using sed Command?
The syntax for deleting multiple lines from the text is as follows.
$ sed 'Line1+d;Line2+d' File-Name
Here’s an example of deleting line numbers 1 & 5 using the sed command.
$ sed '1d;5d' file
As seen above, lines (1 & 5) are deleted, and you can replace (1 & 5) with any number(s) that you wish to delete.
How to Delete a Range of Lines Using sed Command?
Using the sed command, you can delete the range of lines from the text where you need to provide the starting and end of the range, as in this format.
$ sed 'Line1,Line2+d' File-Name
As an example, the below command will delete all the lines from “1” to “”.
$ sed '1,5d' file
As seen above, the lines ranging from (1 to 5) are deleted, and you can replace these numbers with any line number you wish to delete between them.
How to Delete All Lines Except Specified Lines (Range) using sed Command?
The sed command can also delete all lines from the text except the ones in the range it is told not to delete. It has the following syntax.
$ sed 'Starting-Line-No,Ending-Line-no+!d' File-Name
Now, let’s delete the lines except lines one to five using this command.
$ sed '1,5!d' File-Name
As you can see in the above image, the lines ranging from one to five aren’t deleted, while the rest are deleted.
How to Delete Lines based on a Pattern using sed Command?
Using the sed command, users can delete lines based on a pattern matched with the text before deletion in the following format.
$ sed '/Pattern-To-Match/d' File-Name
Let’s delete the lines with the text ‘the’ in them using this command.
$ sed '/the/d' dummy.txt
As seen in the above image, the lines having the words ‘The’ in them are deleted, which in our case resided on lines (7 & 9).
How to Delete Blank Line(s) from the Text using sed Command?
The blank or empty lines in the text aren’t of any use, so this syntax is used to delete them using the sed command.
$ sed '/^$/d' File-Name
Let’s use the above command to delete the blank lines in our text.
$ sed '/^$/d' file
As seen in the above image, we’ve deleted the blank/empty lines from the text.
That’s how the blank lines can be deleted using the sed command
The sed command provides a variety of functionalities to delete lines in Linux. We can delete a single line, multiple lines, and the lines falling in a specific range. All these operations are carried out using the line numbers, and apart from that, the empty lines can also be deleted by utilizing the regular expressions in the sed command.