Basic Bash algorithm with sum/subtraction

Hi all,
i'm making some test on a data file. Imagine i have two columns inside it :

80377,20

80377,20

80379,19

80378,20

80380,20

80382,20

80381,21

Just to understand how can it works, imagine to subtract 100 to the number in the first column when the other one in the second column changes by one. For example >> if changes from 20 to 21 then add 100 to 80381, if it changes from 20 to 19 subtract 100 to 80381..that's it.

Any idea it can be done with a shell script while reading a whole file?

Thanks in advance .

A few things to note here:-
As you are using 'bash' your data shows us pseudo-floating point arithmetic.
Are we to assume that anything beyond the floating point can be ignored?
If it can't be ignored do we round up or down, or both depending on the floating point value?
Why are there commas, (field separators?), on the last two lines?

Hi,
of course, i've corrected the output as it should be.
thank you

So you have every other an empty line. What should your output look like? Does it have to be shell syntax, or would awk , sed , ..., do as well?

Well, shooting in the dark, try

while IFS=, read NR SC; do [ "$NR" != "" ] && { ((NR+=100*(SC-${OLDSC:-$SC}))); OLDSC=$SC; echo $NR,$SC; } || echo; done < file
2 Likes