Deleting Specific Lines from a File in Linux Command Line
Here are a few usecases of deleting specific lines from a text file using the sed command.
The task is simple. You have to delete specific lines from a text file in Linux terminal.
Using commands like rm deletes the entire file and you don't want that here.
You can use a text editor like Vim or Nano, enter the file and delete the desired lines. However, this approach is not suitable for automation using bash scripts.
Instead, you can use the powerful sed command line editor and delete lines that match specific criteria. And of course, you can use sed in your shell scripts.
Let me show a few use cases.
Remove nth line from the file
Imagine you have to delete the 7th line number in a file. You can use the sed command like this:
sed -i '7d' filename
Let me explain how it works:
-i: This option enables the in-place editing. By default, sed will only display the output. With this
-ioption, it modifies the actual file without showing it on the display.
7d: Here 7 is the line number and
dinstructs the deletion of the line.
filename: This is the file you want to modify. You may also provide absolute or relative path if the file is in some other directory.
Let me show with a real example. Take a look at the content of the file named agatha.txt:
The Mystery of the Blue Train The Seven Dials Mystery The Murder at the Vicarage Giant's Bread The Floating Admiral The Sittaford Mystery Peril at End House Lord Edgware Dies
To remove the 4th line from this file, I use:
sed '4d' agatha.txt
This will result in the following display with the line 'Giant's Bread' removed :
The Mystery of the Blue Train The Seven Dials Mystery The Murder at the Vicarage The Floating Admiral The Sittaford Mystery Peril at End House Lord Edgware Dies
Now, let's say you want to remove the first line of the file in Linux command line. Use sed like this:
sed -i '1d' filename
-ioption of sed command.
Remove the last line using sed
You learned to delete a particular line but what if you need to remove the last line?
You may always get the total number of lines in a file using wc command and use this number with sed. However, sed has a dedicated way of deleting the last line of a file. You don't need to worry about the number of files anymore now.
sed -i '$d' filename.txt
Delete a range of lines
Similar to what you saw earlier, you can also delete a range of lines from the file.
Suppose you have to delete the lines from 11 to 15, you can provide the range like this:
sed -i '11,15d' filename
Note that it will also remove the lines 11 and 15, not just the lines that fall between them.
Remove lines containing a string
You can also use sed to delete all the lines that contain a given string or match a specific pattern.
The command below will delete all the lines that contain the word 'string':
sed -i '/string/d' filename
Do note that this is a case-sensitive search. You may also use a regex pattern instead of a regular string.
Remove lines starting with a word
If you want to remove all the lines starting with a particular word or letter, you just have to provide the regex pattern like this:
sed -i '/^word/d' filename
Bonus Tip: Remove all empty lines
Before I end this article, let me quickly share how you can remove all empty lines using sed:
sed -i '/^$/d' filename
I hope you find this quick tip helpful in removing lines from text file in Linux command line.
If you are interested, learn more on sed command line editor: