Update information to my column in awk

154 views Asked by At

I have two files - in each file is two column and many rows.. I need to update information from second file and overwrite it in first file.

For example:

File1: File2

A 1       B 7
B 2       C 8
C 3       D 9
D 4       E 10 
E 5       H 1
F 6       I 7 
G 7

And I need to add new values from second file and update existing values:

Final_file:

A 1
B 7
C 8
D 9
E 10
F 6
G 7
H 1
I 7  

I hope my question is clear. Important is, that both file doesnt have the same number of rows and rows are not matching.

I was try something like this :

awk 'NR==FNR { a[$1]=$2; next} $1 in a {print $0, a[$1]}' File1 File2 

but ouput are just matching columns:

B 2 7
C 3 8
D 4 9
E 5 10

Can anybody please help fix my code to keep all informations update!!

Thank you so much for nay help!

1

There are 1 answers

5
Kent On BEST ANSWER
awk '{a[$1]=$2}END{for(x in a)print x, a[x]}' f1 f2

you don't need check if $1 in a. because you just want to simply overwrite the data if $1 was already in a.

so, just overwrite it.