This should work, although I assume you made a small mistake in your input. Based on your third template, the following data should be:
Instead:
in file-2 case I need to get: 0.1666 0.1666 0.3333 # because |1-2/(1+2+3)| and |2-3/(1+2+3)| and |1-3/(1+2+3)|
It should be:
in file-2 case I need to get: 0.1666 0.3333 0.1666 # because |1-2/(1+2+3)| and |1-3/(1+2+3)| and |2-3/(1+2+3)|
Here is awk one liner:
awk ' NF{ a=0; for(i=1;i<=NF;i++) a+=$i; for(j=1;j<=NF;j++) { for(k=j;k<NF;k++) printf("%s ",-($j-$(k+1))/a) } print ""; next; }1' file
Short version:
awk ' NF{for (i=1;i<=NF;i++) a+=$i; for (j=1;j<=NF;j++){for (k=j;k<NF;k++) printf("%2.4f ",-($j-$(k+1))/a)} print "";a=0;next;}1' file
Input file:
[jaypal:~/Temp] cat file 1 2 1 2 3 1 2 3 4
Test:
[jaypal:~/Temp] awk ' NF{ a=0; for(i=1;i<=NF;i++) a+=$i; for(j=1;j<=NF;j++) { for(k=j;k<NF;k++) printf("%s ",-($j-$(k+1))/a) } print ""; next; }1' file 0.333333 0.166667 0.333333 0.166667 0.1 0.2 0.3 0.1 0.2 0.1
Test from a shorter version:
[jaypal:~/Temp] awk ' NF{for (i=1;i<=NF;i++) a+=$i; for (j=1;j<=NF;j++){for (k=j;k<NF;k++) printf("%2.4f ",-($j-$(k+1))/a)} print "";a=0;next;}1' file 0.3333 0.1667 0.3333 0.1667 0.1000 0.2000 0.3000 0.1000 0.2000 0.1000
source share