Home Linux Commands How to Remove Lines from a File Using Sed Command

How to Remove Lines from a File Using Sed Command

streaming editor (sed) is an important tool when you work with parsing and transforming text in your nix-based systems. It is used for finding, filtering, text substitution, and text manipulations such as insertion, deletion, replace, and search in the text files.

[ You might also like: How to Use Sed to Find and Replace String in Files ]

In most Linux distributions, the sed command comes pre-installed and you can verify it using the following commands, which will show the binary location of the command and version.

$ which sed
$ sed --version

Here in this article, I am going to show you how to remove lines from a file using the sed command with the help of a sample file that contains 7 lines. I am going to use this file for demonstration purposes.

$ cat testfile.txt

First line
second line
Third line
Fourth line
Fifth line
Sixth line
SIXTH LINE

How to Delete a Line from a File

To delete the line from a file you can use the below command. You have to substitute 'N' with the line number and 'd' is to delete the line.

$ sed 'Nd' testfile.txt

If you have to delete the fourth line from the file then you have to substitute N=4.

$ sed '4d' testfile.txt
Delete Line from File
Delete Line from File

How to Delete First and Last Line from a File

You can delete the first line from a file using the same syntax as described in the previous example. You have to put N=1 which will remove the first line.

$ sed '1d' testfile.txt

To delete the last line from a file using the below command with ($) sign that denotes the last line of a file.

$ sed '$d' testfile.txt
Delete First and Last Lines from File
Delete First and Last Lines from File

How to Delete Range of Lines from a File

You can delete a range of lines from a file. Let’s say you want to delete lines from 3 to 5, you can use the below syntax.

  • M – starting line number
  • N – Ending line number
$ sed 'M,Nd' testfile.txt

To actually delete, use the following command to do it.

$ sed '3,5d' testfile.txt
Delete Range of Lines from-File
Delete Range of Lines from-File

You can use ! symbol to negate the delete operation. This will delete all lines except the given range(3-5).

$ sed '3,5!d' testfile.txt
Negate Operation
Negate Operation

How to Remove Blank Lines from a File

To delete all blank lines from a file run the following command. An important point to note is using this command, empty lines with spaces will not be deleted. I have added empty lines and empty lines with spaces in my test file.

$ cat testfile.txt

First line
second line
Third line
Fourth line
Fifth line
Sixth line
SIXTH LINE
$ sed '/^$/d' testfile.txt
Lines with Spaces Not Removed
Lines with Spaces Not Removed

From the above image, you can see empty lines are deleted but lines that have spaces are not deleted. To delete all lines including spaces you can run the following command.

$ sed '/^[[:space:]]*$/d' testfile.txt
Lines with Spaces Removed
Lines with Spaces Removed

[ You might also like: How to Delete Empty Lines in Files Using Grep, Sed, and Awk ]

How to Delete Lines Starting with Words in a File

To delete a line that starts with a certain word run the following command with ^ symbol represents the start of the word followed by the actual word.

$ sed '/^First/d' testfile.txt

To delete a line that ends with a certain word run the following command. The word to be deleted followed by the $ symbol will delete lines.

$ sed '/LINE$/d' testfile.txt
Delete Line Start with Words in File
Delete Line Start with Words in File

How to Make Changes Directly into a File

To make the changes directly in the file using sed you have to pass -i flag which will make the changes directly in the file.

$ sed -i '/^[[:space:]]*$/d' testfile.txt 

We have come to the end of the article. The sed command will play a major part when you are working on manipulating any files. When combined with other Linux utilities like awk, grep you can do more things with sed.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.