31
Jan

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.

[email protected] $ 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.

[email protected]$ sed -i '/foobar/d' garbage.txt
[email protected]$ cat garbage.txt
This is a plain text file that
contains lots of text
and it's unnecessary

[email protected]$

Feel free to donate if this post prevented any headaches! Another way to show your appreciation is to take a gander at these relative ads that you may be interested in:


There's 0 Comment So Far

Share your thoughts, leave a comment!