Grep command to find string in Variable in file2

Hi ,
I am executing 2 queries and output is saved in file1.txt and file2.txt
example of file1.txt

Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,,
Testing Excel _.xlsx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,,
example of file2.txt
Testing Excel _.xlsx,,,,,b,20653
Testing word Doc.docx,,,,,a,20653

Expected output:file3.txt

Testing word Doc.docx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,,,,,,,a,20653
Testing Excel _.xlsx,/Lab/Development and Validation/Multitest/MT_010708/Testing,Development and Validation,root,11-Mar-2014,,,,,,,,,,b,20653

Now I want to merge file1.txt and file2.txt into file3.txt using following code with the help of name highlighted above which is common in both files and appears in the begining of line.

 
while read line
do
file_met_name=`echo $line | cut -d ',' -f1`
met_line_2 =` grep "$file_met_name,",file2.txt | cut -d ',' -f2-`
echo "$line","$met_line_2" >> file3.txt
done < file1.txt

Problem faced: If $file_met_name has string with multiple spaces as in "Testing word Doc.docx" the above code is not able to provide required output.

If order doesn't matter and you can sort file1.txt and file2.txt by their first field....

sort -t, -k 1,1 -o file1.txt file1.txt
sort -t, -k 1,1 -o file2.txt file2.txt

So.. if the files are sorted then you can use join to do the "merge"

join -t, -j 1 file1.txt file2.txt >file3.txt

Not sure if this is exactly what you wanted, but might be...

Instead of giving us a program that doesn't give the output you want, tell us the output you do want.

With few adjustments your code seems to work

while read line
do
file_met_name=`echo "$line" | cut -d ',' -f1`
met_line_2=`grep "^$file_met_name," file2.txt | cut -d ',' -f2-`
echo "$line","$met_line_2"
done < file1.txt > file3.txt

The following splits on IFS for reading so you save one cut :

while IFS="," read file_met_name other
do
met_line_2=`grep "^$file_met_name," file2.txt | cut -d ',' -f2-`
echo "$file_met_name,$other,$met_line_2"
done < file1.txt > file3.txt

With awk it becomes much faster but needs more memory:

awk -F, 'NR==FNR {s[$1]=substr($0,index($0,FS)); next} {print $0 s[$1]}' file2.txt file1.txt > file3.txt