sed delete line


In this article I want to explain how you can use sed to automatically delete a line from a file or a stream. This can be useful in the following cases:

  • delete the first line of a file to implement a simple resource pool (e.g. available IP addresses can be managed in a simple text file. To get a new one out of the pool – simply pop the first line of the file)
  • delete the last line of a file to implement a simple LIFO structure (LastInFirstOut) in a simple file. This file could even be versioned by subversion or git to have it available on multiple systems.
  • delete a line that matches a pattern – (e.g. to remove all comments from a file)

For each of these sample use cases I will try to give you a short example. If you don’t know “sed” you can read this article to get started. Or this article to remove newlines from a file.

I will use the following file to demonstrate the examples:

[email protected]:~$ cat sed-example.txt
first line ... blabla
second line ... blibli
third line ...
blurred lines ... lalala
last line

sed-delete-line

delete the first line of a file

The following command prints the file contents to stdout – and removes the first line (but the file itself is not saved):

[email protected]:~$ sed '1d' sed-example.txt 
second line ... blibli
third line ... 
blurred lines ... lalala
last line

To really edit the file and remove the first line persistently you can add the “-i” option (= edit inplace. Note: this option might not work in older versions of sed):

[email protected]:~$ sed -i '1d' sed-example.txt 
[email protected]:~$ cat sed-example.txt 
second line ... blibli
third line ... 
blurred lines ... lalala
last line

delete the last line of a file

The following command prints the file contents to stdout – and removes the last line (but the file itself is not saved):

[email protected]:~$ sed '$d' sed-example.txt 
first line ... blabla
second line ... blibli
third line ... 
blurred lines ... lalala

To really edit the file and remove the last line persistently you can add the “-i” option (= edit inplace):

[email protected]:~$ sed -i '$d' sed-example.txt 
[email protected]:~$ cat sed-example.txt 
first line ... blabla
second line ... blibli
third line ... 
blurred lines ... lalala

delete a line of a file that matches a pattern

The following command prints the file contents to stdout – and removes the line that matches the pattern “blurred line” (but the file itself is not saved):

[email protected]:~$ sed '/blurred line/d' sed-example.txt 
first line ... blabla
second line ... blibli
third line ... 
last line

To really edit the file and remove “blurred line” persistently you can add the “-i” option (= edit inplace):

[email protected]:~$ sed '/blurred line/d' sed-example.txt 
[email protected]:~$ cat sed-example.txt 
first line ... blabla
second line ... blibli
third line ...
last line 

other more complex examples

Remove empty lines:

sed '/^$/d' sed-example.txt

Delete third to fifth line:

sed '3,5d' sed-example.txt

Remove all commented lines (assuming your comments start with #):

sed -e 's/#.*$//' sed-example.txt

Delete all xml style comments (). (single lines and multiple lines)

sed -e '//d' -e '//d' sed-example.xml