I am trying to get data from a file that is like this:
6 6 1 0
0.1166667E+02 0.4826611E-09 0.4826611E-09 0.3004786E-09 0.5000000E-15
1.000000000000000E-004
CAR
system-001
10.51965443 -34.96542345 301 1.95329810 1.00000000
-15.558 0.1631E+01 0.1597E+02
-15.407 0.1661E+02 0.1779E+02
-15.255 0.4253E+01 0.1990E+02
-15.104 0.0000E+00 0.2000E+02
-14.952 0.0000E+00 0.2000E+02
-3.884 0.0000E+00 0.2000E+02
-3.732 0.0000E+00 0.2000E+02
-3.581 0.0000E+00 0.2000E+02
-3.429 0.0000E+00 0.2000E+02
-3.277 0.8214E-03 0.2000E+02
-3.126 0.3543E+00 0.2002E+02
1.726 0.1019E+01 0.4386E+02
1.877 0.5581E+00 0.4399E+02
2.029 0.0000E+00 0.4400E+02
2.181 0.0000E+00 0.4400E+02
2.332 0.0000E+00 0.4400E+02
2.484 0.0000E+00 0.4400E+02
2.636 0.0000E+00 0.4400E+02
2.787 0.0000E+00 0.4400E+02
2.939 0.0000E+00 0.4400E+02
3.090 0.0000E+00 0.4400E+02
3.242 0.0000E+00 0.4400E+02
3.394 0.0000E+00 0.4400E+02
3.545 0.0000E+00 0.4400E+02
3.697 0.0000E+00 0.4400E+02
3.849 0.0000E+00 0.4400E+02
4.000 0.0000E+00 0.4400E+02
4.152 0.6271E-01 0.4400E+02
4.303 0.4520E+01 0.4433E+02
4.455 0.5040E+01 0.4511E+02
I want to take always the fourth column from the 6 line (1.95329810 in this case), then look for its closest value in the following lines, from the first column(1.877 in this case). That only for referencing, after founding that, I want to extract the next line which its second column is non zero (4.152).
So I would like to get 1.95329810 and 4.152 as output, so I can substract them and get:
band_gap=4.152-$fermi_energy
By taking in consideration @DopeGhoti s answer, I used his code with an if statement:
#!/bin/bash
fermi_energy=$(awk 'NR==6 {printf $4}' DOSCAR-62.4902421.st)
awk -f go.awk DOSCAR-62.4902421.st
Where the go.awk file is:
BEGIN {
test=0
}
NF == 3 && test == 0 && $2 != "0.0000E+00" {
keptvalue=$1
}
NF == 3 && test == 0 && $2 == "0.0000E+00" {
#print keptvalue
test=1
}
NF == 3 && test == 1 && $2 != "0.0000E+00" {
if ( sqrt(($fermi_energy-$1)**2) < 0.5 )
{
print $1
test=0
}
}
But I think that it is not the right way to use bash variables inside an awk script.
P.D. In the case you are wondering, the data represents the calculations of the Density Of States of the electrons of an oxide. The first column represents the electron's energies, the second the electron's amount in that energy level. Therefore, when looking for the next non '0.0000E+00' value since the closest level of the Fermi Energy, we can calculate the energy required to make the electrons jump and conduct electricity. (Metals have zero band gap, thus they do not need energy input to conduct electricity)