loop and extract matching filename

i am unable to exact matching filename in the loop.

Filename.in file contains

Customer_Product_
Information_Customer_Product_
sale_Product_

/home_dir contains files

CUST_INFO_990090_1111.csv
"1","Customer Product Detail","1000","salary"
"1","Information Customer Product Detail","1001","salary"
"1","Customer Product Detail","1002","salary"
"1","Information Customer Product Detail","1003","salary"
"1","saleProduct Detail","1004","salary"

Script:

for file1 in `ls /home_dir/|grep CUST_INFO|grep csv`
do
  SID=`echo $file1 | cut -d '_' -f4 | awk -F"." '{print $1}'`
  PID=`echo $file1 | cut -d '_' -f3`

    for file in `cat /home_dir/filename.in`
    do
        var1=`echo $file"Detail"`
        var2=`echo $var1|  awk '{gsub(/_/, " ");print}'`
        grep "$var2" /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`
    done
done

Finally i am creating the 3 files as required for business logic.

1) Information_Customer_Product_990090_1111.csv

"1","Information Customer Product Detail","1001","salary
"1","Information Customer Product Detail","1003","salary

2) sale_Product_990090_1111.csv

"1","saleProduct Detail","1004","salary

3) Customer_Product_Detail_990090_1111.csv

"1","Customer Product Detail","1000","salary
"1","Information Customer Product Detail","1001","salary
"1","Customer Product Detail","1002","salary
"1","Information Customer Product Detail","1003","salary

Now the 3) file having both (Customer Product Detail and Information Customer Product Detail) information.
As per the requirement i need to fetch only Customer Product Detail information in this file.

i am exactly having problem here

Tried with many options.i am unable to extract the filename beacuse the file itself having double quotes.It is not fetching the exact value.

grep "$var2" /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`
grep -w "$var2" /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`
grep -w '"$var2"' /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`
awk -F"," '{ if ( $0 ~ /"$var2"/ ) print $0}'  /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`

Any help greatly appricated.

Thanks
Suri

---------- Post updated at 09:39 PM ---------- Previous update was at 07:16 AM ----------

Hi,

I have 3 files

1) Information_Customer_Product_990090_1111.csv
2) Customer_Product_Detail_990090_1111.csv
3) sale_Product_990090_1111.csv
Example:
Based on file name =�Customer_Product_Detail� I am fetching data.
As I am I getting two files when I am trying to fetch �Customer_Product_Detail� information.
I am fetching two files information when I try to fetch �Customer_Product_Detail�

Information_Customer_Product_990090_1111.csv
Customer_Product_Detail_990090_1111.csv

I want to fetch exact file name in the script.
With in the for loop i using the below command for fetching filename.

grep "$var2" /home_dir/$file1 > `echo /home_dir/$file"Detail_"$PID"_"$SID".txt"`

any help greatly appricated.

Thanks
Suri

Try...

grep "[^ ]$var2" .....

Thanks Ygor

It is working fine.

This issue is closed.