sed: Copying part of output into a table

Viewed 87

I use G09 to calculate electron proprieties of molecules to use in my master degree. I have a lot of output files and would be good if i put all data that i need into a single file.

My idea is to use sed command to select the data and than copy to another file. However it is not working well. I will explain why with 3 examples:

  1. This is a part of an output file, i am interested in the value -382.8066162 (HF=-382.8066162):
\\Version=ES64L-G09RevD.01\State=1-A'\HF=-382.8066162\RMSD
=4.013e-09\Dipole=0.5408183,-0.047947,0.\Quadrupole=1.1759993,3.185993
9,-4.3619932,-4.1913364,0.,0.\PG=CS [SG(C6H6O2)]\\

So, to do that i use the following command in terminal:

sed '/HF=.*RMSD/ {s/.*HF=//; s/RMSD.*//; b; }; d' < cs.log |tr -d '\' >> teste

And it works very well! But look at others examples of output file.

  1. This is a part of other output file, again i am interested in the value -382.791406:
\\Version=ES64L-G09RevD.01\State=2-A'\HF=-382.79140
6\S2=0.750062\S2-1=0.\S2A=0.75\RMSD=6.390e-09\Dipole=-1.5048474,-0.989
1441,0.\Quadrupole=-14.9615773,1.2107467,13.7508306,9.5370629,0.,0.\PG
=CS [SG(C6H6O2)]\\@

The data skipped a line - the value 6 is in the beginning of the next line - and the word after the value also changed (to S2).

  1. This is a part of other output file, again i am interested in the value HF=-382.3893192:
\\Version=ES64L-G09RevD.01\State=2-A'\HF=-382
.3893192\S2=0.750091\S2-1=0.\S2A=0.75\RMSD=4.227e-09\Dipole=-1.8688747
,-0.9712963,0.\Quadrupole=-15.2914168,1.4139864,13.8774304,10.0476251,
0.,0.\PG=CS [SG(C6H6O2)]\\@

Again, the data HF=-382.3893192 was separeted into two lines. The first part HF=-382 and the second part .3893192.

How can i fix it?

Thanks

3 Answers

Could you please try following, written and tested with shown samples in GNU awk. Written and tested in link https://ideone.com/DCfS2a

awk '
match($0,/HF=[-]?[0-9]+([.][0-9]+)?/){
  print substr($0,RSTART+3,RLENGTH-3)
}
'  Input_file

To pass multiple Input_file(s) to above awk program and could take output by appending > output_file eg---->

awk '
match($0,/HF=[-]?[0-9]+([.][0-9]+)?/){
  print substr($0,RSTART+3,RLENGTH-3)
}
'  Input_file1 Input_file2 > output_file

EDIT: Its late night here so written and tested this with shown samples Only in ideone site https://ideone.com/CLG26G to handle newline coming in matched value, there could be other ways as of now I could think of this regex style only.

awk'
BEGIN{ RS="" }
{
  while(match($0,/HF=[-]?[0-9]+([.][0-9]+)?([\n] +([.][0-9]+))?/)){ 
   val=substr($0,RSTART,RLENGTH)
   sub(/\n */,"",val)
   print val
   val=""
   $0=substr($0,RSTART+RLENGTH)
  }
}
' Input_file

With GNU awk for the 3rd arg to match() and tested against your 3rd sample input:

awk -v RS= 'match($0,/HF=([^\\]+)/,a){gsub(/\s/,"",a[1]); print a[1]}' file
-382.3893192

Using gnu awk, you can do this:

awk -v RS='\\\\HF=' 'NR>1{gsub(/\\.*|[[:space:]]+/, ""); print}' file
-382.8066162
-382.791406
-382.3893192
Related