Improve script made to calculate value based on present and previous line

Hi all,

I have made at small script to make a simple calculation on a file which is formatted in this way:

I want to create a new file in which the value of particular line minus the previous line is printed.

So my wanted output is:

I have made the following program to do the job but it is very slow (and my file has 6 million lines):

#!/bin/bash

FILE="$1"

VALUEBEFORE=0

while read line
do

VALUE=`echo $line | awk '{print($2)}'`
NAME=`echo $line | awk '{print($1)}'`

TEST=`echo $VALUE $VALUEBEFORE | awk '{print($1 - $2)}'`

VALUEBEFORE=`echo $line | awk '{print($2)}'`

echo "$NAME   $TEST"

done < $FILE

Can any suggest how the script can work faster?
Thanks.

You can give this a try:

#!/bin/bash

FILE="$1"

awk 'NR==1{print;next}!val{val=$NF;print ;next}{p=val;val=$NF;$NF= $NF-p}1' $FILE > newfile
1 Like

Thanks Franklin52

I saw you also posted this code which is more simple:

awk 'p{print $0-p}{p=$0}' file

So I am using this instead as I better understand what is happening as I did not really understand the "!val".
Best

Simpler and surely faster, no calls to external programs.

#!/bin/bash
OLDVAL=0
while read NAME VALUE
do
   echo "$NAME $((VALUE-OLDVAL))"
   OLDVAL=$VALUE
done <"$1"

awk -v s=0 'NR==1{print;next}{print $1,$2-s;s=$2}' infile
awk  'NR-1{x=y;y=$NF;$NF=$NF-x}1' file