String Comparision

I want to compare two strings using awk dynamically without trimming the spaces and want to find the count of matching string.

Input Strings file:
File1 content (file1):
" a        "
" a2       "
File2 content (file2):
" a        "
" a        "
" a2       "
" b2       "
" c2       "

Please correct the following code i want exact string matching and not similar matching without trimming the spaces.

while read uid
do
{
 cn=`cat file2 | awk -v k=$uid '$1 ~ k' |wc -l`
        if [ $cn -ge 1 ]
        then
   echo "String and count=$uid  $cn"
       fi
} 
done < file1

:wall:

How big are the files?
Can't you just use grep

$ cat f3
" a        "
" a2       "
$ 
$ cat f4
" a        "
" a        "
" a2       "
" b2       "
" c2       "
$ 
$ grep -f f3 f4
" a        "
" a        "
" a2       "
$ 

maybe -F is also needed.

grep -F -f file1 file2

hi
the below on ur expecting

#!/bin/bash
###String matching between files
for i in `cat file1 | awk -F " " '{ print $2 }'`
   do
 for j in `cat file2 | awk -F " " '{ print $2 }'`
   do
     if [ $i = $j ]
        then
        echo " String $i in both the files"
        else
        echo " String $i not in both files"
        fi
   done
done