Home > Net >  take difference between 2nd column values using awk
take difference between 2nd column values using awk

Time:04-23

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?

Thanks in advance!

CodePudding user response:

You may use this awk:

awk '{$3 = (NR > 1 ? ($2-p)*13.6 : "Ediff(eV)")} {p=$2} 1' 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
  • Related