Reputation:
File 1.txt
:
13002:1:3:6aw:4:g:Dw:S:5342:dsan
13003:5:3s:6s:4:g:D:S:3456:fdsa
13004:16:t3:6:4hh:g:D:S:5342:inef
File 2.txt
:
13002:6544
13003:5684
I need to replace the old data in column 9 of 1.txt
with new data from column 2 of 2.txt
if it exists. I think this can be done line by line as both files have the same column 1 field. This is a 3Gb file size. I have been playing about with awk but can't achieve the following.
I was trying the following:
awk 'NR==FNR{a[$1]=$2;} {$9a[b[2]]}' 1.txt 2.txt
Expected result:
13002:1:3:6aw:4:g:Dw:S:6544:dsan
13003:5:3s:6s:4:g:D:S:5684:fdsa
13004:16:t3:6:4hh:g:D:S:5342:inef
Upvotes: 1
Views: 64
Reputation: 189357
You seem to have a couple of odd typos in your attempt. You want to replace $9 with the value from the array if it is defined. Also, you want to make sure Awk uses colon as separator both on input and output.
awk -F : 'BEGIN { OFS=FS }
NR==FNR{a[$1]=$2; next}
$1 in a {$9 = a[$1] } 1' 2.txt 1.txt
Notice how 2.txt
is first, so that NR==FNR
is true when you are reading this file, but not when you start reading 1.txt
. The next
in the first block prevents Awk from executing the second condition while you are reading the first file. And the final 1
is a shorthand for an unconditional print
which of course will be executed for every line in the second file, regardless of whether you replaced anything.
Upvotes: 2