Grep for a word or word with underscore

I have a file "test" with following contents:

cat test 

abc
abcd_efg
abc_abc

I want to only grep for abc or abc_ without getting other results, how do I achieve this?

If I use

grep -w abc test

option I get only abc and not abc_.
If I use

egrep  "abc|abc_" test

its still printing abcd.

Try

grep -E "abc$|abc_" file
abc
abc_abc
1 Like

Thanks Rudic. It was quick :slight_smile:

what are they for you?

egrep  "abc|abc_" test

Should have given you

abc
abcd_efg
abc_abc

So as we have always asked you to give in your first post, what OS are we dealing with and what shell are you using? to start with...