Get row number from file1 and print that row of file2

Hi.
How can we print those rows of file2 which are mentioned in file1. first character of file1 is a row number.. for eg

file1

1:abc          
3:ghi          
6:pqr          

file2

a abc
b def
c ghi
d jkl
e mno
f pqr             

output should contain

1: a abc
3: c ghi
6: f pqr

Please help me with this case. i believe it can be done using awk

Try:

awk -F"[ :]" 'NR==FNR{a[$2]=1;next}$2 in a{print a[$2]": "$0}' file1 file2

Approach with join if the original file has the same format. This omits the : though.

sed 's/:/ /' file2 | join -1 2 -2 2 -o 1.1,2.1,2.2 - file2

--ahamed

but this limits to this fine only.. i want to read from a file and then do operations in another file

---------- Post updated at 02:06 PM ---------- Previous update was at 02:05 PM ----------

@bartus11
i get following output with your code

1: a
1: b
1: c
1: d
1: e
1: f
1: g
1: h
1:

You mean you have one file as reference file1 and then based on the data in file1 you need to do the operations on other multiple files?

--ahamed

i have many pairs of file1 and file2 each containing different values.. i want a solution so that based on values in file1 i get corresponding rows in file2

Try this...

awk -F'[: ]' 'NR==FNR{a[$2]=$1;next} $2 in a{ print a[$2]":" $0 }' file1 file2

And identify the pair of files and pass it to this command thru some loop.

--ahamed

---------- Post updated at 11:15 AM ---------- Previous update was at 11:13 AM ----------

Which is your OS? If solaris use nawk.
bartus11 code works for me with a minor modification, he missed a $ by mistake. Infact, my code is same as his.

awk -F"[ :]" 'NR==FNR{a[$2]=$1;next}$2 in a{print a[$2]": "$0}' file1 file2

--ahamed