Exclude lines in a file with matches with multiple Strings using egrep

Hi

I have a txt file and I would like to use egrep without using -v option to exclude the lines which matches with multiple Strings.

Let's say I have some text in the txt file. The command should not fetch lines if they have strings something like
CAT MAT DAT

The command should fetch me the line which do have any of these Strings CAT MAT DAT

How can I write the command using egrep without "-v" option.
Thanks in advance.

How can you write the command using egrep without the -v option? With great difficulty. All you have to do is create an ERE that matches any line that does not match any line that contains something like CAT, MAT, or DAT. How you define "something like" may make a huge difference between this being difficult or impossible.

If this isn't a homework assignment (which would need to be filed in the Homework and Coursework Questions forum instead of the Shell Programming and Scripting forum), why would you need to avoid the egrep -v option?

Thanks for response. Actually I have multiple conditions in my egrep to look out for pattern like

line starting with a certain string, spaces at some point, numbers at somepoint, ending with some characters. excluding few strings is one such condition
So I cannot use -v.
This requirement came in for my work Not for assignment.

For matching condition, I can use this
egrep (CAT|MAT|DAT) test.txt

but to negate this, I tried all options like using (^) but couldnt get it.

---------- Post updated at 01:54 PM ---------- Previous update was at 01:54 PM ----------

Thanks for response. Actually I have multiple conditions in my egrep to look out for pattern like

line starting with a certain string, spaces at some point, numbers at somepoint, ending with some characters. excluding few strings is one such condition
So I cannot use -v.
This requirement came in for my work Not for assignment.

For matching condition, I can use this
egrep (CAT|MAT|DAT) test.txt

but to negate this, I tried all options like using (^) but couldnt get it.

How about presenting the entire scenario so a fit solution could be worked out?

1 Like

If really don't want to use -v option with egpre or grep to exclude the lines containing CAT|MAT|DAT strings, then as per my knowledge the best option to use is sed

sed '/string/d' filename

here we can exclude one matching string at time

More details you can find here how to delete one or more lines from given file