Compare multiple columns from 2 files

Hi,

I need to compare multiple columns from 2 files.

I can, for example, have these 2 files:

file1:

col1, col2, col3,col4
a,1,4,7
b,2,5,8
c,3,6,9

file2:

col1, col2, col3,col4
a,2,3,2
b,5,7,5
c,1,9,8

As a result, I need for example the difference between the columns 2 and 4:

col2, col4
1,-5
3,-3
-2,-1

I made something that works for comparing 1 column, but I wonder if it's possible to make it work for multiple columns:

nawk -F, 'FNR==NR{a[$1]=$3;next}{print $3-a[$1]}'  file1 file2
nawk -F, 'FNR==NR{c2[$1]=$2;c4[$1]=$4;next}{print $2-c2[$1],$4-c4[$1]}' file1 OFS=, file2
1 Like

Thanks, that makes sense..

awk -F, 'FNR==NR{a[$1]=$2;b[$1]=$4;next}{if($2 !~ /[a-z]/){print $2-a[$1],$4-b[$1]}else{print $2,$4}}' OFS=, file1 file2