I have a file contains numbers.
1 34 44 44 46
5 35 40 40 45
6 36 28 30 40
My goal is for each column to have the absolute value after subtracting the following value by the previous one as the below:
1 34 44 44 46
4 1 4 4 1
1 1 12 10 5
I used
cat File.txt | awk '{for(i=1; i<=$i-1 ;i++)$i=(a[i]-=$i)}END{print $1 >=0 ? $1 : 0 - $1}'
but I got only the absolute value of the subtractions of the first column. I tried also the $0 instead of $1 but i didn’t get the correct output. Does anyone has an idea how to improve the above command in order to get my desired output?
Advertisement
Answer
test.awk
function abs(x){ return ((x < 0.0) ? -x : x) } { for(i=1; i<=$i ;i++) {a[i]=abs(p[i]-$i)} } { for(i=1; i<=$i ;i++) {p[i]=$i;} } /[0-9]/{ for (i in a) {printf "%s ",a[i]};print "";print "" }
run as follows:
awk -f test.awk <path_to_your_file>