Skip to content

111 Recipes

How to remove file lines that match a pattern

Published ·UPD ·In Recipes

Using GNU sed:

$ sed -i '/pattern/d' ./file

To remove the lines and keep a copy of the original:

$ sed -i.bak '/pattern/d' ./file

Since the pattern is a regular expression, special characters should be escaped. For example, to remove all lines containing

$ sed -i '/example\.com/d' ./file