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

Time:04-02

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

CodePudding user response:

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
  • Related