'take difference between 2nd column values using awk

I have a data file, let's call it fileA. It has two columns and I want to take difference of 2nd column and multiply the output with 13.6 and write it down in 3rd column with top Ediff(eV) should be written.

FileA

36 -448.21483529
40 -448.23331592
44 -448.24002943
48 -448.24322255
50 -448.24407044
52 -448.24486713
.
.
.
.

Desired output

36 -448.21483529  Ediff(eV)
40 -448.23331592 -0.251337
44 -448.24002943 -0.0913037
48 -448.24322255 -0.0434264
50 -448.24407044 -0.0115313
52 -448.24486713 -0.010835
.
.
.
.

half working solution

after searching I found this answer which is able to solve my problem but I need to put units as well. Please help me with that.

awk 'NR>1{$3=$2-p} {p=$2} 1' FileA -------found solution
awk 'NR>1{$3=($2-p)*13.6} {p=$2} 1' FileA -------- multiplied with 13.6

output after multiplying with 13.6

36 -448.21483529
40 -448.23331592 -0.251337
44 -448.24002943 -0.0913037
48 -448.24322255 -0.0434264
50 -448.24407044 -0.0115313
52 -448.24486713 -0.010835

How to add Ediff(eV) at the top of Ediff values?



Solution 1:[1]

You may use this awk:

awk '{print $0, (NR > 1 ? ($2-p)*13.6 : "Ediff(eV)"); p=$2}' fileA

36 -448.21483529 Ediff(eV)
40 -448.23331592 -0.251337
44 -448.24002943 -0.0913037
48 -448.24322255 -0.0434264
50 -448.24407044 -0.0115313
52 -448.24486713 -0.010835

Solution 2:[2]

Slightly different code between gawk and mawk pertaining to how they track ++NF

CODE

mawk '$++NF=!_<NR?13.6*(-__+(__=$(NF-!_))):substr("Ediff(eV)",(__=$(NF-!_))~_)'

gawk '$++NF=!_<NR?13.6*(-__+(__=$NF)):substr("Ediff(eV)",(__=$NF)~_)'

OUTPUT

36 -448.21483529 Ediff(eV)
40 -448.23331592 -0.251337
44 -448.24002943 -0.0913037
48 -448.24322255 -0.0434264
50 -448.24407044 -0.0115313
52 -448.24486713 -0.010835

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 RARE Kpop Manifesto