sed command to delete lines in linux

Sed Command to Delete Lines in Linux

Sometimes you may need to delete one or more lines from file in Linux. Sed is a very useful command for this purpose. It allows you to edit text input in numerous ways. In this article, we will look at sed command to delete lines in Linux.


Sed Command to Delete Lines in Linux

Sed command accepts stream input text and modifies it according to user requirements. This input may be provided via text file or standard output of another command.

Let us say you have the following text file in Linux.

$ cat data.txt
1. Today
2. is
3. a
4. beautiful
5. day


Now we will look at different use cases to delete lines using sed command.

Delete Single Line

If you want to delete a single line you can specify its line number followed by ‘d’ after sed keyword. For example, here is the command to delete 3rd line of your file.

$ sudo sed '3d' data.txt
1. Today
2. is
4. beautiful
5. day

If you want to delete the last line, you can use $d instead of a specific number.

$ sudo sed '$d' data.txt
1. Today
2. is
3. a
4. beautiful

Please note, if you don’t specify the line number, then sed command will delete all lines in your file.


Delete Range of Lines

If you want to delete a continuous range of line, say, line 1-3, you can specify them as 1,3d as shown below.

$ sudo sed '1d,3d' data.txt
4. beautiful
5. day


Delete Multiple Lines

If you want to delete multiple non-continuous lines, you can mention the lines to be deleted one after the other, separated by semi-colon (;) delimiter. Here is an example to delete line 1, 3, and last line

$ sudo sed '1d;3d;$d' data.txt
2. is
4. beautiful

You can even combine ranges and discontinuous lines. Here is an example to delete lines 1, 3-5

$ sudo sed '1d;3d,5d' data.txt
2. is


Delete Lines Except specified ones

If you want to delete all lines except the ones you specify, then use ! operator to exclude specified lines. Here is an example to delete all lines except 2-4.

$ sudo sed '2,4!d' data.txt
2. is
3. a
4. beautiful


Delete Empty Lines

You can also use regular expressions to delete based on specific patterns. In this case, we will use them to delete empty lines. You need to specify your regular expression between /../ operators.

$ sed '/^$/d' data.txt

In the above example ^ means starting with and $ means ending with. Since they are next to each other, it stands for empty lines.


Delete Lines based on pattern

As mentioned above you can delete lines based on regular expressions. Just mention it between /../. Here is an example to delete lines containing ‘day’

$ sed '/day/d' data.txt
2. is
3. a
4. beautiful

Similarly, if you want to delete lines containing any of the specified patterns, you can mention them together with a | separator. Here is the sed command to delete lines containing either ‘is’ or ‘ful’ string

$ sed '/is\|ful/d' data.txt
1. Today
3. a
5. day

Similarly, you can delete lines starting with a particular string using ^ character. Here is the command to delete lines starting with ‘day’ string

$ sed '/^day/d' data.txt

You can also delete lines ending with specific string by adding $ after that string. Here is a sed command to delete lines ending with ‘day’ string.

$ sed '/day$/d' data.txt
2. is
3. a
4. beautiful


Delete lines based on keywords

sed command also supports various keywords to help you quickly manipulate strings. You need to specify them within [: :]. Here is the command to delete lines containing digits in them.

$ sed '/^[[:digit:]]/d' data.txt

Similarly, you may also use [:upper:] and [:lower:] keywords to delete lines containing uppercase and lowercase characters respectively.

Here is sed command to delete lines starting with capital letter.

$ sed '/^[[:upper:]]/d' data.txt

Similarly, here is the sed command to delete lines starting with lower case letter.

$ sed '/^[[:lower:]]/d' data.txt

We have covered the common uses cases to delete lines in Linux. These are only a small sample of things you can do with sed command. sed command can not only delete text but also replace it. It is simple but so powerful that there are full-fledged books about sed command. Its support for regular expressions and easy integration with scripting languages makes sed command really useful.

Also read:

How to Install & Use Wine in Ubuntu Linux
How to Iterate over Multiple Lists in Parallel in Python
How to List All Files in Directory in Python
How to Send HTML Email with Attachment in Python
Postfix Mail Configuration Step by Step

Leave a Reply

Your email address will not be published. Required fields are marked *