To create a copy of the file without lines matching "cat" or "rat", one can use
grep in reverse (
-v) and with the whole-word option (
grep -vwE "(cat|rat)" sourcefile > destinationfile
The whole-word option makes sure it won't match
grateful for example. Output redirection of your shell is used (
>) to write it to a new file. We need the
-E option to enable the extended regular expressions for the
Alternatively, to remove the lines in-place one can use
sed -i "/\b\(cat\|rat\)\b/d" filename
\b sets word boundaries and the
d operation deletes the line matching the expression between the forward slashes.
rat are both being matched by the
(one|other) syntax we apparently need to escape with backslashes.
sed without the
-i operator to test the output of the command before overwriting the file.
(Based on Sed - Delete a line containing a specific string)