0

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!

Geroge
  • 561
  • 6
  • 17

1 Answers1

1
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.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Thank you so much..And can I keep sorting? And it is possible to check, if it works right? There is thousands rows.. I would like to count how nany updates I did and how many new values was add.. I am appreciate all your help!!! – Geroge Jan 22 '14 at 13:01
  • And maybe if I can separate the updates one from the new ones :-) This would be very helpful... Thank all of you again.. – Geroge Jan 22 '14 at 13:11
  • @user2881857 pipe to `sort`? – Kent Jan 22 '14 at 13:34
  • Yeah its work - sort -k1 > new_file...But can I separate which value are new ones (which were add to the first file - in example H1 and I7).. – Geroge Jan 22 '14 at 13:40
  • @user2881857 if you open your file2, that would be the "new ones". if that is what you want to have, your question should be rephrased like "append file1 to file2, remove dup-key lines from file1" ? – Kent Jan 22 '14 at 15:15