sed delete lines not containing specific string

sed
user1246172 picture user1246172 · Mar 3, 2012 · Viewed 79.9k times · Source

I'm new to sed and I have the following question. In this example:

some text here
blah blah 123
another new line
some other text as well
another line

I want to delete all lines except those that contain either string 'text' and or string 'blah', so my output file looks like this:

some text here
blah blah 123
some other text as well

Any hints how this can be done using sed?

Answer

potong picture potong · Mar 3, 2012

This might work for you:

sed '/text\|blah/!d' file
some text here
blah blah 123
some other text as well