script to match patterns in 2 different files.

I am new to shell scripting and need some help. I googled, but couldn't find a similar scenario.

Basically, I need to rename a datafile. This is the scenario -
I have a file, readonly.txt that has 2 columns - file# and name.
I have another file,missing_files.txt that has id and name. Both the files are tab delimited.
This is how the 2 files look -

readonly.txt -

1|/u01/oradata/smp1.dbf
552|/u15/oradata/smp32.dbf
83|/u12/oradata/gdf12.dbf
24|/u01/oradata/ryt.dbf
52|/u01/oradata/yrr11.dbf

missing_files.txt-

00552|missing00552.dbf
00083|missing00083.dbf
00001|missing00001.dbf
00024|missing00024.dbf
00052 |missing00052.dbf

Now, this is what I want -
loop through missing_files.txt and if the first column matches the first column in readonly.txt (by ignoring the leading 0s in the second file), then, give me the second column of that line.
For eg, the last line in readonly.txt is 52|/u01/oradata/yrr11.dbf. Now the first column matches the first column of the last line in missing_files.txt (It should not match the first line in missing_files.txt-since 00552 also has 52 in it), then I need to output missing00052.dbf.

This is the script that I have now -

for i in `cat missing_files.txt`
do
fileid=$(grep $i missing_files.txt | cut -d"|" -f1)
missing_filename=$(grep $i missing_files.txt | cut -d"|" -f2)
filename=$(grep $fileid $smartp1_readonlydbfiles.txt | cut -d"|" -f2)
echo $filename
done

The

filename=$(grep $fileid $smartp1_readonlydbfiles.txt | cut -d"|" -f2)

doesn't do what I want.

Can someone please help?

Hi

 awk -F"|" 'NR==FNR{a[$1]=$2;next}{ if(int($1) in a)print a[int($1)];}' readonly.txt missing_files.txt

Guru.

Guru, thanks a lot. I tried it and it looks like I don't need to put this in the for loop. I actually need to get the missing file name (eg. missing00552.dbf) and the corresponding filename (/u15/oradata/smp32.dbf) so that it can be passed into sqlplus to do a -

alter database rename file 'missing00552.dbf' to '/u15/oradata/smp32.dbf';
nawk -F'|' 'FNR==NR{f1[$1+0]=$2;next} $1+0 in f1 {print "alter database rename file " q $2 q " to " q f1[$1+0] q ";" }' q="'" readonly.txt missing_files.txt