Searching columns and subtracting values in awk

Hi everyone, I had a similar question a couple days ago but my problem has gotten significantly (to me anyway) more complex.

I have two files:

File 1:

0808 166 166 62 9 0
1000fights 1 1 2 1 0
100places2visit 2 2 2 2 0
10veronica91 167 167 3 1 0
11thgorgeous 346 346 3806 1461 122
12angelal 24 24 5 0 1
15minbeauty 1201 1201 2164 478 45
1happybecky 1318 1318 663 164 7
2bears 908 908 694 171 38
2cre8 125 125 305 93 8

File 2:

0808 195 195 20 0 104
0minali0 1 1 0 0 0
0p0g3kw 2 2 17 0 75
1000fights 1 1 4 0 5
100places2visit 17 17 40 0 111
10veronica91 178 178 1 0 476049835
11thgorgeous 450 450 3363 0 9115
12angelal 271 271 31 0 147
12sheeshee 6 6 7 0 38
159159159 3 3 6 0 13

Values for column 1 might be in file 1 and not file 2, or in file 2 and not file 1.

I want awk to look at file 1 and save those column 1 values,
then match those values with the corresponding row in file 2 and then calculate the differences between these numbers. so, for example, line 1's desired
output would be.

0808 29 29 -42 -9 -104

A couple of caveats. If a value for column 1 is not in both files, I don't care about it. Also, the solution doesn't have to be awk, or fast or efficient. I just need something that works!

Thanks so much.

awk     'NR==FNR   {tmp[$1]=$0;
                    next
                   }
         $1 in tmp {split(tmp[$1],ci);
                    print $1, $2-ci[2], $3-ci[3], $4-ci[4], $5-ci[5], $6-ci[6]
                   }
        ' file1 file2

Your sample result should have 104 in the last column.

1 Like

This works great, its exactly what I needed. Thanks so much!