Delete matching lines in files with sed

 Wed, 17 Apr 2024 18:04 UTC

Delete matching lines in files with sed
Image: CC BY 4.0 by cybrkyd


I really need to make a note of this command as I always end up searching the web every time I need to do this.

Delete all lines with a matching pattern using the sed command

When faced with a find-and-delete problem across multiple files, this sed command always saves my day.

$ sed -i '/pattern to match/d' ./infile

To explain, sed -i instructs to perform the command in-file.

'/pattern to match/d' — the search string followed by “d” to delete.

./infile — is the location of the files. You can get creative by using wildcards. For example:

$ sed -i '/meta property/d' *.html

will remove all lines containing the phrase “meta property” from any .html file in the current directory.