i am trying to compare two files (f1,f2) and create a third one f3. if field $1 is the same in both files then generate an sql query like this :

file f1 

```

db1
db2
db3

```

file f2

```

db1 tab11
db1 tab12
db1 tab13
db2 tab21
db2 tab22
db3 tab31
db3 tab32
db3 tab33
db3 tab34

```

file f3

```

    ( TRIM(C.DATABASENAME) = 'db1' AND C.TABLENAME IN ('tab11', 'tab12','tab13')) OR
    ( TRIM(C.DATABASENAME) = 'db2' AND C.TABLENAME IN ('tab21', 'tab22')) OR
    ( TRIM(C.DATABASENAME) = 'db3' AND C.TABLENAME IN ('tab31', 'tab32', 'tab33','tab34' ))

```

Is it possible to create the file f3 with awk ? 

this is what i came up with but it generates a file wit the wrong format of data

```
while read db; do
awk -v mydb=$db '{if ($1=mydb) printf "( TRIM(C.DATABASENAME) ="$1 "AND C.TABLENAME IN (" $2 "," }' f2 > f3
done < f1

```