title: xyz coordinates manipulation using sed or awk
body: <p>I have a huge number of plain text files containing Cartesian xyz coordinates of chemical structures. A sample could look like that:</p>
  
  <pre><code>B                 -1.38372433    0.56274955    2.22204795 
  B                  0.01637488    1.69210489    1.81167819
  B                  0.29103422   -0.35499374    0.15388510
  B                  1.14485163    0.19631678    1.74992009 
  Fe                -0.92583118    1.01775624    0.27450973
  S                 -0.35374797   -1.05624221    1.74656393
  C                 -1.87367299    1.66919492   -1.27526252
  O                 -2.42173866    2.04584255   -2.17123145
  H                 -2.54747585    0.75818308    2.22742141
  H                  0.62677160   -0.81072498   -0.88156036 
  H                  0.38495881    2.74424131    2.19841880
  H                  2.25808628    0.09159351    1.37282254
  </code></pre>
  
  <p>In this case, each H atom is bonded to a B atom with a distance of 1.18 angstroms. What I'm supposed to do is to change, in turn, each BH vertex by a P vertex. 
  Using bash, I'd like to act on all text files at once by taking the coordinates of the first B atom encountered and use it as a point of origin of a sphere and search within a radius of 1.18 Angstroms for the bonded Hydrogen atom, delete this H atom with its coordinates then change the B into a P atom.</p>
  
  <p>An expected output of the above sample would be something like that:</p>
  
  <pre><code>P                 -1.38372433    0.56274955    2.22204795 
  B                  0.01637488    1.69210489    1.81167819
  B                  0.29103422   -0.35499374    0.15388510
  B                  1.14485163    0.19631678    1.74992009 
  Fe                -0.92583118    1.01775624    0.27450973
  S                 -0.35374797   -1.05624221    1.74656393
  C                 -1.87367299    1.66919492   -1.27526252
  O                 -2.42173866    2.04584255   -2.17123145
  H                  0.62677160   -0.81072498   -0.88156036 
  H                  0.38495881    2.74424131    2.19841880
  H                  2.25808628    0.09159351    1.37282254
  </code></pre>
  
  <p>I've done something similar a while back, but that was adding xyz coordinates of a H atom at a distance of 1.2 Angstroms from an existing B atom. what I used back then was:</p>
  
  <pre><code>for i in *.inp; do awk '/^B / { print; if (++count == 1) printf("%-10.8f %-14.8f %-14.8f %s\n", "H", $2+1.2, $3+1.2, $4+1.2); next } { print }' $i &gt; temp/`basename $i`--H.inp; done
  </code></pre>
  
  <p>However, I'm still not successful in coming up with something similar to solve my current problem.</p>
  
  <p>Any help is really appreciated
  Thanks in advance</p>