31
Jan
Posted by Derek@TheDailyLinux » Add Comment »
Delete Lines Within File Containing Matching Pattern/Text
Here’s a quick one-liner command that will remove any lines matching a text patter from a file:
sed -i '/pattern/d' filename
For example, say I want to remove all lines in the file garbage.txt that contain the text “foobar”. Here is my console output.
user@localhost $ cat garbage.txt This is a plain text file that contains lots of text but most of it is foobar and it's unnecessary to include foobar in text. user@localhost$ sed -i '/foobar/d' garbage.txt user@localhost$ cat garbage.txt This is a plain text file that contains lots of text and it's unnecessary user@localhost$