Sed Command to Delete a Line

29

Sed is a built-in Linux tool for text manipulation. The term sed stands for “stream editor”. Despite the name, sed isn’t a text editor by itself. Rather, it takes text as input, performs various text modifications according to instructions, and prints the output.

This guide will demonstrate how to use sed to delete a line from a text.

Sed on Linux

The full name of sed gives us a hint at its working method. Sed takes the input text as a “stream”. The text can come from anywhere – a text file or standard output (STDOUT). After taking the input, sed operates on it line by line.

For demonstration, here’s a simple text file I’ve generated.

Deleting line using sed

To delete a line, we’ll use the sed “d” command. Note that you have to declare which line to delete. Otherwise, sed will delete all the lines.

Delete single line

The following sed command will delete the first line of the text.

Sed Command to Delete a Line Sed Command to Delete a Line 1627235083 525 Sed Command to Delete a Line

Basically, to delete a line, you need the line number of the target line. Let’s remove line 5.

Sed Command to Delete a Line Sed Command to Delete a Line 1627235084 456 Sed Command to Delete a Line

To delete the last line of the text file, instead of manually calculating the line number, use “$” instead.

Sed Command to Delete a Line Sed Command to Delete a Line 1627235084 543 Sed Command to Delete a Line

Delete a range of line

Sed can delete a range of lines. Here, the minimum line value is 1, and the maximum line value is 5. To declare range, we’re using comma (,).

Sed Command to Delete a Line Sed Command to Delete a Line 1627235085 450 Sed Command to Delete a Line

Delete multiple lines

What if the lines you desire to remove are not in a fixed range? Have a look at the following sed command. Note that we’re using a semicolon (;) as the delimiter. Essentially, each delimited option is a separate sed command.

$ sed ‘1d;2d;3d;$d’ dummy.txt

Sed Command to Delete a Line Sed Command to Delete a Line 1627235085 745 Sed Command to Delete a Line

Delete all lines except specified range

In the next example, sed will only keep the lines described by the range. Here, “!” is the negation operator, telling sed to keep the specific lines.

Sed Command to Delete a Line Sed Command to Delete a Line 1627235086 607 Sed Command to Delete a Line

Delete empty lines

If there are multiple empty or blank lines in the text, the following sed command will remove all of them.

Sed Command to Delete a Line Sed Command to Delete a Line 1627235086 968 Sed Command to Delete a Line

Delete lines based on pattern

Sed can search for a particular pattern and perform the specified actions on the line. We can use this feature to delete specific lines that match a pattern.

Let’s have a look at the following demonstration. Sed will remove any line that contains the string “the”.

Sed Command to Delete a Line Sed Command to Delete a Line 1627235086 323 Sed Command to Delete a Line

We can also describe multiple strings to search for. Each string is delimited using the symbol “|”.

$ sed ‘/brown|the/d’ dummy.txt

Sed Command to Delete a Line Sed Command to Delete a Line 1627235087 670 Sed Command to Delete a Line

Delete lines starting with a specific character

To denote the starting of a line, we’ll use the caret (^) symbol.

The following sed command will remove all the lines starting with a digit. Here, the character group “[:digit:]” describes all digits (0-9).

$ sed ‘/^[[:digit:]]/d’ dummy.txt

Sed Command to Delete a Line Sed Command to Delete a Line 1627235087 986 Sed Command to Delete a Line

We can also describe multiple characters for a valid match. The following example will match all the lines that start with “t” and “b”.

$ sed ‘/^[tb]/d’ dummy.txt

Sed Command to Delete a Line Sed Command to Delete a Line 1627235088 89 Sed Command to Delete a Line

In the next example, check out how to remove all the lines that start with an uppercase character. Here, we’re using the uppercase character group “[:upper:]”.

$ sed ‘/^[[:upper:]]/d’ dummy.txt

Sed Command to Delete a Line Sed Command to Delete a Line 1627235088 335 Sed Command to Delete a Line

If the target lines have lowercase characters at the start, use the lowercase character group “[:lower:]”.

$ sed ‘/^[[:lower:]]/d’ dummy.txt[/

Sed Command to Delete a Line Sed Command to Delete a Line 1627235089 503 Sed Command to Delete a Line

Delete lines ending with specific character

To denote the end of a line, we can use the symbol “$”. It describes the match with the last occurrence of the pattern.

In the next example, sed will delete lines ending with “e”.

Sed Command to Delete a Line Sed Command to Delete a Line 1627235089 185 Sed Command to Delete a Line

Let’s try with a multiple-character search.

$ sed ‘/[en]$/d’ dummy.txt

Sed Command to Delete a Line Sed Command to Delete a Line 1627235089 200 Sed Command to Delete a Line

Deleting lines that match the pattern and the next line

We’ve already demonstrated how to delete a line if a pattern matches. We can further extend and delete the subsequent line as well.

Check out the following sed command.

$ sed ‘/the/{N;d;}’ dummy.txt

Sed Command to Delete a Line Sed Command to Delete a Line 1627235090 515 Sed Command to Delete a Line

Sed will match the line that contains “the” and delete the subsequent line as well.

Deleting line from the pattern match to the end

We can further extend the previous example to order sed to delete all the lines, starting from the first match of the pattern.

$ sed ‘/fox/,$d’ dummy.txt

Sed Command to Delete a Line Sed Command to Delete a Line 1627235090 911 Sed Command to Delete a Line

Here, sed will delete the line that matches the pattern “the” first and all lines afterward.

Final thought

Sed is a simple tool. However, it can do wonders, thanks to the support for regular expression. Sed also integrates seamlessly in various scripts.

This was just a short guide demonstrating one of the sed functions – deleting lines. There are tons of other things you can do with sed. For example, check out this mega guide on 50 sed examples. It’s a fantastic guide covering all the basics and many advanced sed implementations.

Happy computing!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More