Want to implement VLOOKUP (Excel function) in Unix

Dear All,

i want to implement vookup function which is there in excel into Unix. Suppose i have 2 files. The files are given below.

File1:

MSC	             Cell	SDCA
Patna-1	             12	Bihar
Patna-2	             45	Ranchi
Bhopal-1	             85	Raigarh
Bhopal-2	             66	Indore
Mumbai-1	78	Turbhe
Mumbai-2	25	Nashik
Mumbai-3	45	Dhule

& File2:

MSC		New MSC
Patna-1		Patna1
Patna-2		Patnaomp2
Bhopal-1	             bhopal
Bhopal-2	             bhopal2
Mumbai-1	mum1
Mumbai-2	mumomp2
Mumbai-3	mum3

The required output must like this:

result
MSC		Cell	SDCA
Patna1		12	Bihar
Patnaomp2	45	Ranchi
bhopal		85	Raigarh
bhopal2		66	Indore
mum1		78	Turbhe
mumomp2	25	Nashik
mum3		45	Dhule

Please help me for the same.

Thank u

Try...

awk 'NR==FNR {a[$1]=$2;next} a[$1] {$1=a[$1]} {print}' file2 file1 > file3

You are looking for the join command. It's pesky in that it requires sorted input, so these forums are shock full of examples of doing it without sorting the input for a particular file format. Scaling up, if you have a lot of these, you might want to import them into a proper database.

awk 'NR==FNR { m[$1] = $2; next }
{ $1 = m[$1]; print }' file2 file1

Searching for NR==FNR should get you a few handfuls of examples, as well as the typical follow-up questions and problems.

Thanks.. it solve my problem

After exceuting your code it is giving me out in different format. out file is given below. I want the out in same line.. can u please help??

Output file.
file1
New MSC
Patna1
Patnaomp2
bhopal
bhopal2
mum1
mumomp2
mum3
Cell SDCA
12 Bihar
45 Ranchi
85 Raigarh
66 Indore
78 Turbhe

& required format is
MSC Cell SDCA
Patna1 12 Bihar
Patnaomp2 45 Ranchi
bhopal 85 Raigarh
bhopal2 66 Indore
mum1 78 Turbhe
mumomp2 25 Nashik
mum3 45 Dhule

Pls help....

Worked for me here, did you copy+paste correctly? Does Ygor's code work better for you?

This code is showing errors

awk: syntax error near line 1
awk: bailing out near line 1

Still i am getting same problem. The columns are not coming in same line. Columns are coming one below another. Please help??

The awk syntax error is often a sign that you are using "old awk". If you are on HP-UX or Sun, look around for an XPG4 version, or see if you have nawk, or mawk, or gawk.