'How to remove, from all files recursively, all lines that don't match list of strings

I'm trying to clean a big list of log files from undesired lines and to only leave those who contains some strings. For example:

sunday morning NOPE again
may it be DENSE in such a place
nothing here really 

In such a case only leave lines that contain NOPE or DENSE.

I've tried using sed from examples, but but didn't manage to add an or for a list of strings, such as:

sed -ni.bak '/\NOPE/p' file


Solution 1:[1]

You can use this sed:

sed -E -i.bak '/NOPE|DENSE/!d' file

This will delete all lines that don't have NOPE and DENSE strings.

If you want to match complete words only then use:

gnu-sed command:

sed -i -E '/\b(NOPE|DENSE)\b/!d' file

bsd-sed command:

sed -E -i.bak '/[[:<:]](NOPE|DENSE)[[:>:]]/!d' file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anubhava