Searching the content of one file using the search key of another file

I have two files:
file 1:

hello.com	neo.com,japan.com,example.com
news.net	xyz.com, telecom.net, highlands.net, software.com
example2.com	earth.net, abc.gov.uk 

file 2:

neo.com
example.com
abc.gov.uk

file 2 are the search keys to search in file 1 if any of the search key is found in file 1 it should return the whole line of file 2 such that the desired output is:

hello.com	neo.com,japan.com,example.com
example2.com	earth.net, abc.gov.uk 

I tried with the following awk script but yet I am not getting the desired output.

awk '{FS="\t"} NF==1 {key[$1]=1} NF > 1 {if( ( $2 in key ) ) {print $1 $2} }' file1 file2 

Any suggestions to fix this ?

A much simpler way to do this is:

grep -F -f file2 file1

Your awk script is failing because:

  1. key is empty when you're reading file1 (you need to switch the order of your operands so the keys will be read from file2 before you try to find the keys in file1, and
  2. your keys are single words (e.g., example.com ), but $2 in file1 is a comma separated list of possible keys. The expression ( $2 in key ) will only be true if a single key from file2 is the only key in the list of keys in a line in file1.

Thank you very much for the clear explanation about awk. The grep script works perfectly fine but just one another concern. If in case my output is just the same as above but with a slight change like this, if the search is found it has to return the whole line else just the first column

hello.com    neo.com,japan.com,example.com 
news.net     
example2.com    earth.net, abc.gov.uk

Any suggestions in this ?

Try

awk -F'\t' 'NR==FNR {key[$1];next}  {printf "%s", $1; for (i in key) if ($2 ~ i) {printf "\t%s", $2; break}; printf "\n" }' file2 file1
hello.com    neo.com,japan.com,example.com
news.net
example2.com    earth.net, abc.gov.uk