paste -d $'\n' <(printf %s "$variable1") <(printf %s "$variable2") |
awk '
!/= [0-9]/ {getline;print;next} # pass through lines that contain no numbers
{
split($0, refLineFields) # split the 1st line of each pair into fields
getline # read the 2nd line of the pair (into $0)
for (i=1;i<=NF;++i) # loop over all fields
# Replace numerical fields with the difference between
# their value and the one from the corresponding line,
# Note that assigning to a field causes the input line to be recomposed
# so that $0 then contains the _modified_ line.
if ($i ~ "^[0-9]+$") $i=$i - refLineFields[i]
# Output the modified line.
print
}
'
Note: This will compress multiple adjacent spaces into a single one each.
- Uses
paste
to merge the two strings so that corresponding lines follow each other in the single output string. - The
awk
program then parses the pairs of lines and performs the arithmetic; see the source comments.