Egrep command is not working for me

my file is below

REREGISTER is something to Failed to create the request
Failed to create the request in not easy
I know how REREGISTER

command i run is

 egrep 'REREGISTER|Failed|to|create|the|request' test1

expected output

REREGISTER is something to Failed to create the request

i should get only first line as output but i am getting all the line.please advise what is wrong i am doing

One way.

grep 'REREGISTER' test1 | grep 'something'

A much more advanced way is to use negative lookahead.

Suppose you want to eliminate lines with the word "bar" in them:

egrep '^(?!.*bar).*$'  filename

I'm not sure which one would be best, but the first one is easier to apply to your example. If you have a large number of lines then it becomes much harder to get a clean answer to what you should do.

BTW - your original syntax says any one of the following words ( in your pipe delimited section) is a match print it.

1 Like

Hello mirwasim,

It is showing like that output because you are giving an OR condition which means any of these words if they are coming into a line then print it(so that is why other than 1st lines are getting printed too because any of the words are coming there), if words sequence will be same as per shown sample Input_file then following may help you in same.

egrep 'REREGISTER.*Failed.*to.*create.*the.*request'    Input_file

Thanks,
R. Singh

2 Likes