Replacement code

Hello everybody,

I am new to this forum and i need some input for the below issue

I have 3 files

file 1

a  mumbai    32344
m  bangalore 4211
x  delhi          4345
e  chennai     4312
d  punjab       5255

file 2

b
t
j
c
d

data in file 2 will be compared with column 1 of file 1 and if it matches data of file3 will be replaced with the data in column 2 of file2

file 3

kolkatta
nagpur
goa
chennai
punjab

Thanks,
New Shellscripter

Hi New Shellscripter,

provide me your expected output..

Thank you Bharat for the quick reply.

Let me frame the question once more with a new dataset with the expected output

Hello everybody,

I need a help on this.
I have a 3 files

file 1

a  mumbai    32344
m  bangalore 4211
c delhi          4345
e  chennai     4312
d  punjab       5255

file 2

a
t
j
c
d

data in file 2 will be compared with column 1 of file 1 and if it matches data of file3 will be replaced with the data in column 2 of file2

file 3

kolkatta
nagpur
goa
chennai
haryana

expected output :-

a  kolkatta      32344
m bangalore  4211
c  chennai     4345
e  chennai     4312
d   haryana      5255

Your file2 has no column 2 that could be used for the replacement...?

data in file 2 will be compared with column 1 of file 1, and if it matches, data of file3 will be replaced with the data, in column 2 of file1

I hope i am able to make some sense now.

Try:

awk 'NR==FNR{getline v<f; A[$1]=v; next} $1 in A { $2=A[$1] }1' f=file3 file2 file1

or

paste file2 file3 | awk 'NR==FNR{A[$1]=$2; next} $1 in A { $2=A[$1] }1' - file1

or if with bash/ksh93/zsh you could also use::

awk 'NR==FNR{A[$1]=$2; next} $1 in A { $2=A[$1] }1' <(paste file2 file3) file1
1 Like

Thank you very Scrutinizer for the help.
Code works for me.

It would be highly helpful if you can explain a bit of what is done.

Thanks