For the first time, as p will be valuated as FALSE and will not execute {print $0-p}. However, p will turn to a non-zero/TRUE value when it execute {p=$0}.From second record onwards, p will be TRUE and will evaluate {print $0-p} and print it.Also, it will set the new value(current line) to p
Franklin52's solution is probably the best, simplest, and shortest there is. However, that shouldn't stop unix userland lovers from having some fun
sed 'x;G;1d;y/\n/+/;s/.*/eval(-&)/' data | m4
Only works with integers, as opposed to the AWK which will handle floating point as well, and assumes that the data file consists of nothing but one integer per line.
$ cat data
5
10
12
6
9
12
5
$ sed 'x;G;1d;y/\n/+/;s/.*/eval(-&)/' data | m4
5
2
-6
3
3
-7
In this case, since they are not being modified, there is no point in having two identical copies of an array. You can simplify your approach to the following:
Thank you very much for your replies. Its working fine.
In addition to the above query, I would like to extend the same from 'n' columns. That is, I have a file with 100 lines with 1000 columns, I would like to subtract each line with each other.
I would like to subtract line1 with line1, line2, line3, line4 and line5, line2 with line1, line2, line3, line4 and line5, line3 with....so on ....
(all against each other)