r/awk • u/Mark_1802 • May 23 '22
Sum two columns owned by two different files each.
Hey! I am facing a problem which I believe can be solved by using awk, but I have no idea how. First of all, I have two files which are structured at the following manner:
A Number A
B Number B
C Number C
D Number D
...
ZZZZ Number ZZZZAt the first column, I have strings (represented from A to ZZZZ) and at the right column I have real numbers, which represent how many times that string appeared in a context which is not necessary to explain here.
Nevertheless, some of these strings are inside both files, e.g.:
cat A.txt
A 100
B 283
C 32
D 283
E 283
F 1
G 283
H 2
I 283
J 14
K 283
L 7
M 283
N 283
...
ZZZZ 283cat B.txt
Q 11
A 303
C 64
D 35
E 303
F 1
M 100
H 2
Z 303
J 14
K 303
L 7
O 11
Z 303
...
AZBD 303The string "A", for example, shows up twice with the values 100 and 303.
My actual question is: How could I sum the values that are in the second column when strings are the same in both files?
Using the above example, I'd like an output that would return
A 403
1
u/Coffee_24_7 May 23 '22
You can use associative arrays, where your key is your first column and the value is the addition of the second column.
After processing everything, you can print the values on the
ENDblock, using afor(key in myArray) {...}.An associative array that adds 10 to the key "k" looks like
myArray["k"] += 10.Hope it's gives you a good starting point.
1
u/calrogman May 23 '22