Merging middle column in a file

I want to merge files as described below:

File 1

a;1;abc
b;2;def
c;3;xyz
d;4;pqr
e;5;mno

File 2

b;41
d;77

output

a;1;abc
b;41;def
c;3;xyz
d;77;pqr
e;5;mno

Please suggest how to do this.

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework Questions forum as specified in the special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If this is not a homework item, please explain how this code will be used and tell us what operating system and shell you're using.

No this is not a home work assignment we use flat files to load our user database from multiple locations. Every record contains multiple fields for a user. For a specific set of users, the value coming in File is wrong. For all such users we get another correction file. So for such users we just want to overwrite one field value from correction file before loading it into our database.

32690 pts/2 00:00:00 ksh

Hello Prabal Ghura,

Could you please try following.

awk -F";" FNR==NR'{A[$1]=$0;next} ($1 in A){print A[$1] FS $NF;next} {print}' Input_file2  Input_file1

Output will be as follows.

a;1;abc
b;41;def
c;3;xyz
d;77;pqr
e;5;mno
 

Thanks,
R. Singh

Any attempts/ideas/thoughts from your side?

Howsoever, try also

awk 'FNR == NR {T[$1] = $2; next} $1 in T {$2 = T[$1]}  1' FS=";" OFS=";" file2 file1

Thanks RavinderSingh13, you saved my day

Thanks RudiC.

---------- Post updated at 02:59 PM ---------- Previous update was at 02:58 PM ----------

Thanks RavinderSingh13

---------- Post updated at 03:44 PM ---------- Previous update was at 02:59 PM ----------

RavinderSingh13
A quick question what if we use different delimiters in both files?

Hello Prabal Ghura,

So let's say your 1 file named Input_file2 has delimiter as ; and Input_file1 has delimiter as , , we could set it for both of them. This is just an example, you could try to fine tune it as per your requirements too.

awk  FNR==NR'{A[$1]=$0;next} ($1 in A){print A[$1] FS $NF;next} {print}' FS=";" Input_file2  FS="," Input_file1

Thanks,
R. Singh

Then change the field separator value before reading each file:

awk 'FNR==NR{A[$1]=$2;next} $1 in A{$2=A[$1]} 1' FS='x' Input_file2  FS='y' OFS='y' Input_file1

where x is the field separator in Input_file2 and y is the field separator in Input_file1 (and the output).

If you want a different field separator in the output than you are using as the File 1 input field separator, it is a little more complicated:

awk 'FNR==NR{A[$1]=$2;next} $1 in A{$2=A[$1]} {$1=$1} 1' FS='x' Input_file2  FS='y' OFS='z' Input_file1

where z is the field separator you want in the output file.