Delete Lines Matching a Specific Pattern in a File using SED

0
6952

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here