Grep exact match without period or other special characters

If I have a file like the following

abc.1
abc
abc_1
abc..1
abc*1
abc@1
abc def ghr
def......
ddef 5466 def ed 
def** 123445

I`m trying to find exact words from the list

abc
def
grep -wf file2 file1

I get

abc.1
abc
abc..1
abc*1
abc@1
abc def ghr
def......
ddef 5466 def ed
def** 123445

But I want to return the following 2 lines as exact word matches.

abc
ddef 5466 def ed

Is there any grep option for this?

for exact word matches, try:

awk '
NR==FNR {a[$1]=$1; next;}
{l=0; for (i=1; i<=NF; i++) if ($i in a) {print ; next;}}
' words_file infile

for exact word match count of 1 (as shown), try:

awk '
NR==FNR {a[$1]=$1; next;}
{l=0; for (i=1; i<=NF; i++) ($i in a) ? l++ : 0; if (l==1) print}
' words_file infile
1 Like

thanks a lot for the awk solution, just curious this cant be accomplished using a simple grep syntax with options ?

The following is an attempt:

egrep '(^|[[:blank:]]+)(abc|def)([[:blank:]]+|$)' infile

But this also prints lines with two or more matching words.
Regarding the awk solution, I like the following better

awk '
NR==FNR {a[$1]; next;}
{L=0; for (i=1; i<=NF; i++) if ($i in a) L++;} L==1
' words_file infile

For grep , file words_file has to have the patterns specified exactly. Example:

words_file:

\(\W\)abc\(\W\|$\)
\(\W\)def\(\W\|$\)

then:

grep -f words_file infile