Calculating differences line by line

i don't even know how to explain this. but i'll try my best.

so i have this data:

jime=1860,yime=1.23243
jime=1859,yime=1.23018
jime=1825,yime=1.15371
jime=1849,yime=1.20769
jime=1841,yime=1.1897
jime=1849,yime=1.20769
jime=1842,yime=1.19195
jime=1827,yime=1.15821
jime=1751,yime=0.98726
jime=1746,yime=0.976014
jime=1723,yime=0.92428
jime=1707,yime=0.888291
jime=1695,yime=0.8613
jime=1680,yime=0.82756
jime=1317,yime=0.0110665
jime=1310,yime=-0.00467853
jime=1291,yime=-0.0474151
jime=1013,yime=-0.672719
jime=982,yime=-0.742447
jime=970,yime=-0.769438
jime=956,yime=-0.800929
jime=853,yime=-1.03261
jime=819,yime=-1.10908
jime=797,yime=-1.15857
jime=782,yime=-1.19231

i need to calculate the percentages of the difference of the in fields in each line, with the next line.

for instance, what is the percentage differences (whether negative or positive) between:

jime=1859,yime=1.23018
and
jime=1825,yime=1.15371

then

jime=1825,yime=1.15371
and
jime=1849,yime=1.20769

and so on.

basically compare the line with the line that comes after it.

so in essence, the preferred output would be something like:

jime=1860,yime=1.23243
jime=1859,yime=1.23018,-0.18%
jime=1825,yime=1.15371
jime=1849,yime=1.20769,+4.46969%
jime=1841,yime=1.1897
jime=1849,yime=1.20769,.....
jime=1842,yime=1.19195
jime=1827,yime=1.15821,......
and so on

i used the following to calculate the percentages:

echo 1.15371 1.20769 | awk '{print ($1 - $2) / $2 * 100}'
1 Like
awk -F'=' 'BEGIN {
        CONVFMT="%f"
} ! ( NR % 2) {
        $0 = $0 "," ( v - $NF ) / ( $NF * 100 ) "%"
} NR % 2 {
        v = $NF
} 1 ' file
1 Like

thank you!! looks like this would work. maybe its my calculation that's wrong, but it looks like the percentages are off.

i get this:

jime=1860,yime=1.23243
jime=1859,yime=1.23018,0.000018%
jime=1825,yime=1.15371
jime=1849,yime=1.20769,-0.000447%
jime=1841,yime=1.1897
jime=1849,yime=1.20769,-0.000149%

like take for instance, the above bolded should be:

echo 1.23243 1.23018 | awk '{print ($1 - $2) / $2 * 100}'
0.1829%

Try this:

awk -F= '{prev=$NF; print; getline; print $0","($NF-prev)/$NF*100"%"}' input
1 Like

Round brackets where not placed correctly:

awk -F'=' 'BEGIN {
        CONVFMT="%.4f"
} ! ( NR % 2) {
        $0 = $0 "," (( v - $NF ) / $NF ) * 100  "%"
} NR % 2 {
        v = $NF
} 1 ' file

If you want to subtract the other way around:

awk -F'=' 'BEGIN {
        CONVFMT="%.4f"
} ! ( NR % 2) {
        $0 = $0 "," (( $NF - v ) / $NF ) * 100  "%"
} NR % 2 {
        v = $NF
} 1 ' file
1 Like

the contents of the datafile changed:

jime=1860,yime=1.23243,lime=[Mar-12-(UTC)]
jime=1859,yime=1.23018,lime=[Mar-12-(UTC)]
jime=1825,yime=1.15371,lime=[Mar-12-(UTC)]
jime=1849,yime=1.20769,lime=[Mar-12-(UTC)]
jime=1841,yime=1.1897,lime=[Mar-12-(UTC)]
jime=1849,yime=1.20769,lime=[Mar-12-(UTC)]

i tried manipulating the fields so the script can adapt to this new datafile layout. "$NF" to work on the second column:

awk -F= '{prev=$3; print; getline; print $0","($NF-prev)/$NF*100"%"}' datafile

but i keep getting:

awk: (FILENAME=mile FNR=2) fatal: division by zero attempted

You can split on either = or , and hard-code 4th column:

awk -F"[=,]" '{prev=$4; print; getline; print $0","($4-prev)/$4*100"%"}' input 
1 Like

is there anyway to hardcode the following scenario into the code?

for instance, one line has a value of:

0.00476979

the next line has a value of:

-0.0142208

the percentage difference is =

133.541%

notice it is positive. should be a negative, considering the numbers dropped.

also, what if both numbers are negative:

-0.0285025

and

-0.0477696

the percentage difference is:

40.3334%

should be

-40.3334%

bipinajith posted a code earlier that can do this. but is there a way to make awk do the proper calculation automatically?

basically if its a drop, the percentages should be negative. if it's an increase, the percentages should be positive.

awk -F"[=,]" '{prev=$4; print; getline; print $0","($4-prev)/($4<0?-$4:$4)*100"%"}' input

The highlighted part basically does an absolute value of $4.
Note that you'll get into problems if the value is zero (division by zero).

1 Like