How to calculate the difference between two adjacent columns?

Dear All,

I need to find the difference between two adjacent columns. The file is having 'i' columns and i need to find the difference between two adjacent columns (like $1 difference $2; $2 difference $3; .... and $(i-1) difference $i). I have used the following coding

awk '{ for (i=1; i<NF; i++) if (($i - $i+1) > 10) print 1; else print 0}' input > output

While using the above in the command line, i am getting the values only in the first column of the output file. I need to print the difference in columns.
(for example; output $1 = input $1 difference input $2...etc..)

The problem here is, i need to get the difference between the two adjacent columns (for example: if the first column first element is 10 and the second column first element is 40, then $1 - $2 = -30, which is less than 10, so the output will be 0 in this case) But if you see the difference is 30. So i need to check the difference is > 10 or not.

Is there any difference operator in awk? (like $1 ~ $2).
I need your help in this regard.

Expecting your reply and thanks in advance.

Regards
Fredrick.

Replace $i+1 with $(i+1)

awk '{ for (i=1; i<NF; i++) if (($i - $i+1) > 10) print 1; else print 0}' input > output

Jean-Pierre.

awk 'function abs(val) {return val>0?val:-val } 
for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) print 1; else print 0}' urfile

Thank you "rdcwayx", but the output is having only one column, i need the output in the same was as the inputs are. Can you help me in this regard?

awk 'function abs(val) {return val>0?val:-val } 
for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) print 1; else print 0}' urfile

Warm regards
Fredrick.

awk 'function abs(val) {return val>0?val:-val }
{for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) printf "1 "; else printf "0 "}} {print "\n"}' urfile

Thank you very much for your prompt reply. I have tried with the following code, but i couldn't get the output as columns, it giving me output in a single column.

awk 'function abs(val) {return val>0?val:-val }
{for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) printf "1 "; else printf "0 "}} {print "\n"}' urfile

Can you suggest some other way to get it?
I need the output in the same format (new_column1=col1 diff col2 (0 or 1), new_column2=col2 diff col3 (0 or 1), etc....)

Thanks in advance

Fredrick.

So why not give us the sample input and output directly.

What you need do by yourself is to adjust printf output. Here is the sample:

awk 'function abs(val) {return val>0?val:-val }
{for (i=1; i<NF; i++) {if (abs($i - $(i+1))>10) printf $i" diff " $(i+1)" (1), "; else printf $i" diff " $(i+1)" (0), "}} {print "\n"}' urfile

I have the select output:

tourID:name:buyin:buyout
0001:SundayMillion:210:10000
0002:MondayMillion:115:232

I want the difference between $buyin and $buyout in absolute and return the high value.

Note: the output, the lines are more than the example, 2.

Thanks