We are trying to use awk/bash/sed to get the last third and fourth value of "DV/DL" of multiple files (input1.dat, input2.dat, input3.dat) and print out DV/DL incrementally from "input1.dat", "input1.dat input2.dat", "input1.dat input2.dat input3.dat",etc.

The "input1.dat" output will be

```
"the fourth last DV/DL in input1.dat"  "the third last DV/DL in input1.dat"

```

The "input1.dat input2.dat " output will be

```
"the fourth last DV/DL in input1.dat"  "the third last DV/DL in input1.dat"
"the fourth last DV/DL in input2.dat"  "the third last DV/DL in input2.dat"

```

The example file (input1.dat) is below. In input1.dat we would like to print out "DV/DL" as "-26.2720 2.6879".

```
NSTEP =    50000   TIME(PS) =     450.000  TEMP(K) =   299.02  PRESS =   213.4
Etot   =    -80270.1079  EKtot   =     33984.2399  EPtot      =   -114254.3478
BOND   =     22963.8665  ANGLE   =      2030.3803  DIHED      =      4232.0101
1-4 NB =       953.1576  1-4 EEL =     12610.8610  VDWAALS    =     20829.7086
EELEC  =   -177874.3319  EHBOND  =         0.0000  RESTRAINT  =         0.0000
DV/DL  =       -26.2168
EKCMT  =     10196.0704  VIRIAL  =      8501.7871  VOLUME     =    367647.0872
                                                Density    =         1.0555
Ewald error estimate:   0.3945E-04
------------------------------------------------------------------------------

------------------------------------------------------------------------------
  A V E R A G E S   O V E R   50000 S T E P S

NSTEP =    50000   TIME(PS) =     450.000  TEMP(K) =   299.79  PRESS =     2.0
Etot   =    -80333.0001  EKtot   =     34071.3290  EPtot      =   -114404.3291
BOND   =     22903.8667  ANGLE   =      2081.5119  DIHED      =      4220.2140
1-4 NB =       932.2502  1-4 EEL =     12548.7169  VDWAALS    =     20862.9158
EELEC  =   -177953.8046  EHBOND  =         0.0000  RESTRAINT  =         0.0000
**DV/DL  =       -26.2720**
EKCMT  =     10189.4552  VIRIAL  =     10173.6800  VOLUME     =    367385.1338
                                                Density    =         1.0562
Ewald error estimate:   0.4704E-04
------------------------------------------------------------------------------

R M S  F L U C T U A T I O N S

NSTEP =    50000   TIME(PS) =     450.000  TEMP(K) =     1.28  PRESS =   145.5
Etot   =       231.2453  EKtot   =       145.6638  EPtot      =       184.9663
BOND   =       151.1551  ANGLE   =        35.0649  DIHED      =        19.6523
1-4 NB =        12.6972  1-4 EEL =        33.8995  VDWAALS    =       174.9784
EELEC  =       305.7204  EHBOND  =         0.0000  RESTRAINT  =         0.0000
**DV/DL  =         2.6879**
EKCMT  =        78.6868  VIRIAL  =      1152.6836  VOLUME     =       406.3321
                                                Density    =         0.0012
Ewald error estimate:   0.3520E-04
------------------------------------------------------------------------------

**DV/DL**, AVERAGES OVER   50000 STEPS

NSTEP =    50000   TIME(PS) =     450.000  TEMP(K) =     0.00  PRESS =     0.0
Etot   =         0.0000  EKtot   =         0.0000  EPtot      =       -26.2720
BOND   =         0.0000  ANGLE   =         0.0000  DIHED      =         0.0000
1-4 NB =         0.0000  1-4 EEL =        -8.4978  VDWAALS    =         0.0000
EELEC  =       -17.7743  EHBOND  =         0.0000  RESTRAINT  =         0.0000
DV/DL  =       -26.2720
Ewald error estimate:   0.0000E+00

    ___________________

```

The following one is our first attempt. 

```
for X in 1 2 3

do

var1='awk -f capture_dvdl.awk'
var2="$var2 input${X}.dat"
var3=">> output${X}.dat"

echo "$var1 $var2 $var3"  > average_preparation.sh

sh average_preparation.sh

done

```

capture\_dvdl.awk is here

```
#!/bin/awk

BEGIN{}
{
if ($1 ~ /^DV\/DL/ && FNR == 2603) {

printf("%14.4f",$3)

 }

 }

 {
 if ($1 ~ /^DV\/DL/ && FNR == 2618) {

 printf("%14.4f\n",$3)

 }

 }

```

This will generate average\_preparation.sh as 

```
awk -f capture_dvdl.awk input1.dat > output1.dat

awk -f capture_dvdl.awk input1.dat input2.dat > output2.dat

```

However, the last third and fourth "DV/DL" are not necessary in the line of 2603 and 2618, so this trick doesn't work. 

The second trick is this 

```
 awk '$1 ~ /^DV\/DL/ {printf("%14.4f\n",$3)}' input1.dat input2.dat input3.dat | tail -n 4 | head -n 2 > output.dat 

```

However, this one will only print out the last third and fourth DV/DL of input3.dat. 

Wonder if any guru could kindly offer any comment? Thanks!