Pattern match using grep between two files

Hello Everyone ,

I have two files. I want to pick line from file-1 and match with the complete data in file-2 , if there is a match print all the match lines in file 3. Below is the file

 cat test1.txt
vikas
vikasjain
j ain
testt
douknow
hello@vik@
#  33
||@@ vcpzxcmvhvdsh
dsfdvfdbsvfds judfdsjgfds
cat test2.txt
vik
ain
j ain123
estt
douknow
hello@vik@
vikasjainvikasjain

This is my shell script

cat compare3.sh

#!/bin/ksh
file="test1.txt"
while read line
do


egrep '$line' ./test2.txt >> match_vikas_new.txt
         
done <"$file"

I have trying to get this working but no luck. Apprcieate if you could guide me on this!

It seems to me you have the order of the files reversed and the variable $line cannot be expanded between to single quotes, you need double quotes.

Another way may be:

grep -Ff test2.txt test1.txt

The -F option forces fixed strings instead of regular expressions.