Sure thing...
fileA.txt contains:
12~13~14~15
16~83~10~44
75~84~96~56
fileB.txt contains:
22~22~32~11
73~85~52~43
74~51~14~68
The code:
paste file[AB].txt | sed 's/ /~/g' | \
awk '
BEGIN { FS="~" }
{
divs=NF/2
for ( i = 1; i <= divs; i++ ) {
f1=i
f2=i+divs
diff=$f1-$f2
pct=diff/$f1
printf("%.0f/%.2f ",diff,pct)
}
printf("\n","")
}'
Paste files A and B together, records side by side (the whitespace between them is a tab):
paste file[AB].txt
12~13~14~15 22~22~32~11
16~83~10~44 73~85~52~43
75~84~96~56 74~51~14~68
Replace tabs with tildes (~), so we have one record pair per line:
sed 's/ /~/g'
12~13~14~15~22~22~32~11
16~83~10~44~73~85~52~43
75~84~96~56~74~51~14~68
...which we pipe through awk:
Set the field separator to ~
awk '
BEGIN { FS="~" }
We're assuming that both files contain the same number of records, and that each
record pair in the files contains an equal number of fields. Therefore, we want
to split the whole record, e.g. "12~13~14~15~22~22~32~11" into two parts. So we
set divs equal to half the number of fields:
{
divs=NF/2
In this case, each file has four fields, so we are going to find the differences
of fields 1 and 5, 2 and 6, 3 and 7, and 4 and 8. The "divs=NF/2" method just
lets us determine at run time how many fields we are dealing with:
Field #: 1 2 3 4 5 6 7 8
Record: 12~13~14~15~22~22~32~11
For each joined record pair, we want to perform as many calculations as there are
record pairs. The number of record pairs is determined dynamically for each line:
for ( i = 1; i <= divs; i++ ) {
Set f1 equal to our iterator. This will be the fileA.txt value for that field number.
It gets incremented after each comparison:
f1=i
Set f2 equal to the iterator plus the number of fields in each div, e.g. if i is field 1,
then we want f2 to be field 5. So f2=i+divs --> f2=1+4.
f2=i+divs
Calculate the difference between field1 and field2. This is tricky because we are not
subtracting the value of f2 from the value of f1; f1 and f2 are pointers (i.e. field numbers).
So $f1 is $1 and $f2 is $5, then on the next iteration $f1 is $2 and $f2 is $6, and so on:
diff=$f1-$f2
Same concept -- just take the difference and divide by the fileA value:
pct=diff/$f1
Print output of diff and pct, using formats %.0f and %.2f respectively (this is arbitrary; I
just chose those formats for looks).
printf("%.0f/%.2f ",diff,pct)
After the loop is finished for the record pair, print a new line:
}
printf("\n","")
}'
All done!