How to delete lines from a file using sed

Overview

Today we’re looking at one of the powerfull tools you can add in your Linux toolbelt and yet underrated standard tool – sed. Sed, shortly for stream editor, is a command with which you can do various actions on a file, amongst which are search, find and replace, insert or delete. And “delete” part is what we’re focused at this time, meaning that we’re covering various methods how to delete lines from a file using sed. Without further ado, let’s get our hands busy, so you’ll see how handy sed really is.

How to delete lines with sed

I’ve created testfile.txt file and put some random text inside (“roses are red” jokes) so let the games begin.

delete lines from a file using sed

To delete a certain line you just have to specify line number accompanied with letter ‘d’ (for delete, duh), like this:

sed 3d testfile.txt

delete lines from a file using sed

As you can see, line 3 is no more. Basically, for first line command would be ‘sed 1d name_of_file’ and for last would be ‘sed …’ well that depend of how many lines are there in a file. But, there is a shortcut that saves you from counting lines so command for deleting last line would look like this:

sed '$d' testfile.txt

delete lines from a file using sed

Notice that I had to enclose $d with single quotes? If I hadn’t, sed would do it’s magic on file name:

delete lines from a file using sed

One of special cases is when you don’t enter line number, so sed deletes them all:

sed d testfile.txt

delete lines from a file using sed

Surely by now you have noticed that original text doesn’t change no matter what sed command we execute. That’s how stream editor works. It just filters and refines text making it easier for you to find and extract information you really need (let’s say searching for specific thing through a log file). If you really need to save your results of sed operations, just add –i in your command. Now back on track.

Delete multiple lines, empty lines and paragraphs

Up to now we were describing how to delete a line. We will now see how to delete several lines. Let’s say we want to delete certain lines, say several odd lines, you’ll use the following:

sed ‘1d;3d;5d;7d’ testfile.txt

delete lines from a file using sed

If you decide to remove empty lines, there is a neat trick to remove them without counting on which lines are they:

sed /^$/d testfile.txt

delete lines from a file using sed

Neat, right? What about when you decide to delete continious lines, like, say, complete paragraph? Instead of just numbering line by line, you take first and last line and you delimit them with comma:

sed 9,11d testfile.txt

delete lines from a file using sed

And it works the other way around too, but you have to use ‘not’ operator (‘!’), just like in programming:

sed ‘9,11!d’ testfile.txt

delete lines from a file using sed

Delete lines that start with a character or a word

Until now searches were based on line numbers, but what to do when you have a very long file so it’s quite painful to count the lines? Well, best thing is to pull out lines using a specific term you need, but bear in mind that sed will remove lines with the term you sought. So, once again, we’ll use negation (not operator):

sed ‘/blue/!d’ testfile.txt

delete lines from a file using sed

You can also have more than one term, but you’ll need to delimit them with ‘ \ ’, as in this example:

sed ‘/red\\blue/!d’ testfile.txt

delete lines from a file using sed

Also, you have an option to remove lines with just a starting character:

sed ‘/^\[v\]/’ testfile.txt

delete lines from a file using sed

Voila! You can also combine more characters:

delete lines from a file using sed

You also have another option how to filter required lines, depending on starting character you’ll use:

sed ‘/^\[\[:upper:\]\]/d’ testfile.txt

To delete lines starting with an uppercase character

delete lines from a file using sed

sed ‘/\[\[:lower:\]\]/d’ testfile.txt

To delete lines starting with lowercase character, and

delete lines from a file using sed

sed ‘/^\[\[:digit:\]\]/d’ testfile.txt

Delete lines that start with a number

To delete lines starting with a number (for which I cannot show an example to you since I have no lines starting with a number).

So far we used sed to delete lines starting with specific character, but did you know it can also delete lines ending with specific character? Watch this:

sed ‘/\[ed\]$/d’ testfile.txt

delete lines from a file using sed

Delete lines that contains a speciffic word/keyword

All lines ending with ‘e’ and ‘d’ are gone. There is one particular option you’ll find specially handy. You can make sed delete all lines after the line that has a term you sought:

sed ‘/face/,$d’ testfile.txt

delete lines from a file using sed

Fenomenal, right? If you, by any chance, decide to delete just following line, you’ll have to modify command a bit:

sed ‘/face/{N;d;}’ testfile.txt

delete lines from a file using sed

Summary

And that would be about it when it comes to deleting lines from a file with sed. In some of the following articles there will be more about sed and what can be done with it. Hope you found it interesting and helpful.

Thank you for your time…