Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete from a text file, all lines that contain a specific string?

How would I use sed to delete all lines in a text file that contain a specific string?

like image 677
A Clockwork Orange Avatar asked Mar 23 '11 19:03

A Clockwork Orange


People also ask

How do you delete all occurrences of a list of words from a text file?

I would recommend that you do a Ctrl+F (PC) Command+F (Mac) find all "Ref" and replace with empty string (in other words leave the replace box empty). Hit enter and all done! Hope this helps!


1 Answers

To remove the line and print the output to standard out:

sed '/pattern to match/d' ./infile 

To directly modify the file – does not work with BSD sed:

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

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

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

To directly modify the file (and create a backup) – works with BSD and GNU sed:

sed -i.bak '/pattern to match/d' ./infile 
like image 133
SiegeX Avatar answered Sep 29 '22 17:09

SiegeX