Calculate difference between consecutive data points in a column from a file

Hi,

I have a file with one column data (sample below) and I am trying to write a shell script to calculate the difference between consecutive data valuse i.e

Var = Ni -N(i-1)

0.3141
-3.6595
0.9171
5.2001
3.5331
3.7022
-6.1087
-5.1039
-9.8144
1.6516
-2.725
3.982
7.769
8.88
8.392
2.188
3.13

I seen not to even know where to start from and so I really need some a lot of help please

Thank you
Malandisa

Is this what you want?

awk 's{print ((s>$0)?s-$0:$0-s)}{s=$0}' file
awk 'NR==1{s=$1;next}{print $1-s;s=$1}' infile

-3.9736
4.5766
4.283
-1.667
0.1691
-9.8109
1.0048
-4.7105
11.466
-4.3766
6.707
3.787
1.111
-0.488
-6.204
0.942

Thank you both Danmero and rdcwayx!

I want to make this a learning experience so please be patient with me!

rdcwayx's gives what I am looking for! danmero's approach makes everything positive (but is actually doing the difference just fine, only that it makes it positive) ....somehow I have no clue what is happening in this command! I have some idea what is happening in rdcwayx's command! Thank you again!! I am surely learning shell scripting here, I actually doubted this was possible with awk!!!

daanmero please can I ask a favour that you just explain what the different symbols actually mean and how that combination achieves the result? and is it possible to modify the command so that it preserves the negative in the results? and any advice where I can find a detailed user guide for awk that?

Thank you all!

Here is the user guide for awk, i used by myself.

The GNU Awk User's Guide

In my opinion the difference between -1 and 1 is 2, like that a difference between any two numbers will be positive.
From here I see that you just want to subtract a number from a previous number and in this case the solution provided by rdcwayx will work just fine.

1 Like