File with minimum energy 1 10 20 30 40 2 20 30 45 50 Another file 1 10 11 50 50 2 20 21 60 60 I want: R 11 100 30 R 21 120 5 30 = (50 + 50) - (30 + 40) 5 = (60 + 60) - (45 + 50) Where 100 = 50 + 50 120 = 60 + 60 . #### use strict; # Find the lowest-energy geometry # Prepare array EDRvars0 containg the EDR at each u from that geometrr +y open(F,">results.txt"); print F "# Bond_length Delocalization_length EDR \n"; # Loop over all log files foreach my $f (<*log>){ my $c=`grep -c "Normal term" $f`; chomp($c); # Avoid files that do +dn't converge if($c>0){ # Find the bond length. We assume this is built into the file +name my $R = $f; $R=~s/.log//; $R=~s/.*_//; # Find the U valnes my $Ustr = `grep -A37 "EDR alpha" $f | tail -n35|awk "{print \ +\\$3}"`; my @Uvars = split(/\n/,$Ustr); # Convert them into an array my $NU = scalar(@Uvars); # That array has $NU elements # Find the and sum alpha and beta my $EDRstr = `grep -A37 "EDR alpha" $f | tail -n35|awk "{print + \\\$4+\\\$5}"`; my @EDRvars = split(/\n/,$EDRstr); # Print the outputs foreach my $i(0..$NU-1){ print F sprintf("%8.3E %12.6E %12.6E\n",$R,$Uvars[$i],$EDR +vars[$i]); } } } close(F);