3

I would like to linearly interpolate a single column based on previous and next know values only where data is equal to "---".

Data example:

0
---
---
---
10
---
20
22
15
---
---
---
-15

Wanted output:

0
2.5
5
7.5
10
15
20
22
15
7.5
0
-7.5
-15

I have seen codes that can interpolate based on first column such as date, as described here. And other codes that would do it for step size, like here but they won't work for me. Thank you in advance!

  • I would strongly encourage you to use another language than bash. Python has [scipy](https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html) ([example2](https://glowingpython.blogspot.com/2011/05/how-to-interpolate-set-of-points.html)), [numpy](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.interp.html), and [pandas](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.interpolate.html) modules, all of which implement linear interpolation and would likely be easier to deal with than bash. – jeremysprofile Aug 21 '18 at 18:05
  • Thanks @jeremysprofile but I don't know any python. I just started scripting using bash and awk and still trying to understand the basics of them. – Pavel Diptan Aug 21 '18 at 18:09

1 Answers1

4

awk to the rescue!

$ awk '!/---/ {n=c; while(c&&c--) print v+(n-c)*($1-v)/(n+1); v=$1; print } 
        /---/ {c++}' file

0
2.5
5
7.5
10
15
20
22
15
7.5
0
-7.5
-15
karakfa
  • 66,216
  • 7
  • 41
  • 56