Delete Lines Matching a Specific Pattern in a File using SED

|
Last Updated:
|
|

In this guide, we are going to learn how to delete lines matching a specific pattern in a file using SED. SED is a stream editor that performs basic text filtering and transformations on an input stream (a file or input from a pipeline).

Delete Lines Matching a Specific Pattern in a File using SED

In our previous guide, we covered how to delete lines matching specific patterns in VIM. You can check by following the link below;

Now, let us go through various examples of deleting lines matching specific pattern in a file using SED. For the purposes of demonstration, we will be using the seven colors of rainbow in a file.

cat colors           
This is color red
This is color orange
This is color yellow
This is color green
This is color blue
This is color indigo
This is color violet

Just like in VIM, we will be using the d command to delete specific pattern space with SED. To begin with, if you want to delete a line containing the keyword, you would run sed as shown below.

sed -i '/pattern/d' file

Where option -i specifies the file in place. If you need to perform a dry run (without actually deleting the line with the keyword) and print the result to std output, omit option -i. For example, do delete a line containing the keyword green, you would run;

sed '/green/d' colors
This is color red
This is color orange
This is color yellow
This is color blue
This is color indigo
This is color violet

Similarly, you could run the sed command with option -n and negated p,(!p) command.

sed -n '/green/!p' colors           
This is color red
This is color orange
This is color yellow
This is color blue
This is color indigo
This is color violet

To delete lines containing multiple keywords, for example to delete lines with the keyword green or lines with keyword violet.

sed '/green\|violet/d' colors
This is color red
This is color orange
This is color yellow
This is color blue
This is color indigo

If you need to delete the lines but create a backup of the original file, then use option -i in the format, -i.bak. This will create a .bak of the original file name.

sed -i.bak '/green\|violet/d' colors

To delete all the lines except that contain a specific pattern or keyword;

sed '/green/!d' colors
This is color green
sed '/green\|violet/!d' colors 
This is color green
This is color violet

To delete lines starting with a specific character or pattern, for example, the comment lines starting with # or lines begining with keyword amos.

sed '/^#/d' colors
sed '/^amos/d' colors

To delete lines ending with a specific pattern or a character, for exmple to delete lines starting ending with character o or keyword amos;

sed '/o$/d' colors
sed '/amos$/d' colors

To combine the above such that you can delete lines ending with letter o or keyword amos;

sed '/o$\|amos$/d' colors

To delete blank lines

sed '/^$/d' colors

To delete blank lines or lines that contain one or more white spaces;

sed '/^ *$/d' colors

To delete blank lines or lines that one or more tabs;

sed '/^\t*$/d' colors

Well, this is just the little we could cover about how to delete lines matching a specific pattern in a file using SED. Feel free to add more examples and suggestions in the comments below. Enjoy.

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
koromicha
I am the Co-founder of Kifarunix.com, Linux and the whole FOSS enthusiast, Linux System Admin and a Blue Teamer who loves to share technological tips and hacks with others as a way of sharing knowledge as: "In vain have you acquired knowledge if you have not imparted it to others".

1 thought on “Delete Lines Matching a Specific Pattern in a File using SED”

  1. Was looking for a simple and easy to follow explanation of sed, or another tool for removing lines with ‘strings’ within. I tested 90% of all examples using a copy of /etc/passwd today, while also entering notes and outputs into CherryTree. Original goal was to remove the rhcsa* accounts I had been using on my host laptop. sed -i.bak ‘/^rhcsa/d/’ passwd.test appears to do the job just fine. Nice to see I can also identify which lines will be retained or removed if desired.

    Thank you. Will review more of the website this week.

    Reply

Leave a Comment