Delta from the first digit

Thanks of your suggestions i was able to calculate the delta between some numbers in a column file with .

 awk 'BEGIN{last=0}{delta[NR]=$1-last; last=$1; print $0" "delta[NR]}'

the file was like

499849120.00
500201312.00
500352416.00
500402784.00
500150944.00
499849120.00
500150944.00
500453184.00
499949664.00
500050304.00
500100640.00

but what if i want to use the first element of this list and create the delta between the first element on the list and all the others?

Thanks in advance.

Best regards

Use NR == 1 for the last assignent, and don't modify it any more.

Thanks for your answer.

i've tried with

awk 'BEGIN{last=NR == 1}{delta[NR]=$1-last; last=$1; print $0" "delta[NR]}' filename

but still create same output with delta by each line

and with

awk 'BEGIN{NR == 1}{delta[NR]=$1-last; last=NR == 1; print $0" "delta[NR]}'

it just delete the last .00 decimals

Try (untested)

awk 'NR == 1 {last = $1} {print $0, $1 - last}'

If a value is an exact integer, awk strips decimals by default. Use printf in case you want decimals.

1 Like