Difference between corresponding elements of successive rows

Hi,

I have a file in the following format

a1 b1 c1 d1 
a2 b2 c2 d2
a3 b3 c3 d3
a4 b4 c4 d4

I need a script to find the difference between corresponding values of successive rows. So the output would have one less row than the input file and should look like:

a2-a1 b2-b1 c2-c1 d2-d1
a3-a2 b3-b2 c3-c2 d3-d2
a4-a3 b4-b3 c4-c3 d4-d3

Thanks

tac |awk 'NR==1{l1=$1;l2=$2;l3=$3;l4=$4}NR>1{print l1"-"$1,l2"-"$2,l3"-"$3,l4"-"$4;l1=$1;l2=$2;l3=$3;l4=$4}'|tac

I would like to perform the mathematical operation (subtract) rather than just printing them, i.e. it should print the "actual" values of a2-a1 b2-b1 c2-c1 d2-d1 and so on for rest of the rows.

Hi sajal.bhatia,
Try with:

echo "10 20 30 40
15 30 42 51
18 32 48 60
25 34 50 71" | awk '{
a[NR]=$1;b[NR]=$2;c[NR]=$3;d[NR]=$4
}
END{
for(i=2;i<=NR;i++) 
print a-a[i-1],b-b[i-1],c-c[i-1],d-d[i-1]
}'
5 10 12 11
3 2 6 9
7 2 2 11

Best regards

Another approach:

awk '
NR==1{a=$1; b=$2; c=$3; d=$4; next}
{print $1-a, $2-b, $3-c, $4-d; a=$1; b=$2; c=$3; d=$4}
' file