I have multiple files that contain thousands of lines of the following pattern:
O HOH 1 11.700 -11.906 1.533
H HOH 0 12.561 -12.318 1.596
H HOH 0 11.445 -12.031 0.627
O HOH 2 17.897 8.999 -0.104
H HOH 0 18.444 8.521 0.516
H HOH 0 17.127 8.457 -0.223
O HOH 3 -0.688 10.310 -15.189
H HOH 0 -0.760 9.366 -15.055
H HOH 0 -0.494 10.665 -14.324
O HOH 4 -10.418 3.733 4.069
H HOH 0 -9.928 3.726 4.897
H HOH 0 -11.222 4.218 4.268
O HOH 5 2.655 -8.910 13.338
H HOH 0 2.377 -9.739 13.737
H HOH 0 1.847 -8.531 12.992
O HOH 6 -0.191 -1.010 -19.469
H HOH 0 0.455 -1.460 -18.924
H HOH 0 -0.571 -0.343 -18.895
O HOH 7 14.338 -15.985 -1.467
H HOH 0 15.129 -15.647 -1.886
H HOH 0 13.854 -16.408 -2.175
O HOH 8 -15.510 -7.073 1.857
H HOH 0 -15.526 -6.116 1.874
H HOH 0 -16.341 -7.321 1.464
O HOH 9 9.155 6.561 -7.729
H HOH 0 9.419 7.335 -7.245
H HOH 0 9.943 6.018 -7.768
.
.
.
.
O HOH 999999 9.155 6.561 -7.729
H HOH 0 9.419 7.335 -7.245
H HOH 0 9.943 6.018 -7.768
what I need is to replace each 2 sequential instants of 'H HOH 0' with 'H HOH xxx' where xxx is the value right above the 0s
what I have tried was that, I split the big file into 3-line files using
split -l 3 foo
then using awk I copied the needed value to another file
awk 'NR==1 print $3' > foo--
then tried to use sed to replace the pattern by reading the file foo-- and replacing the ' 0 ' in foo
sed -e '/ 0 /{r foo--' -e 'd}' foo
but that didn't work
pretty sure there is a better way to do this
The end result should look like this:
O HOH 1 11.700 -11.906 1.533
H HOH 1 12.561 -12.318 1.596
H HOH 1 11.445 -12.031 0.627
O HOH 2 17.897 8.999 -0.104
H HOH 2 18.444 8.521 0.516
H HOH 2 17.127 8.457 -0.223
O HOH 3 -0.688 10.310 -15.189
H HOH 3 -0.760 9.366 -15.055
H HOH 3 -0.494 10.665 -14.324
O HOH 4 -10.418 3.733 4.069
H HOH 4 -9.928 3.726 4.897
H HOH 4 -11.222 4.218 4.268
O HOH 5 2.655 -8.910 13.338
H HOH 5 2.377 -9.739 13.737
H HOH 5 1.847 -8.531 12.992
O HOH 6 -0.191 -1.010 -19.469
H HOH 6 0.455 -1.460 -18.924
H HOH 6 -0.571 -0.343 -18.895
.
.
.
O HOH 99999 -0.191 -1.010 -19.469
H HOH 99999 0.455 -1.460 -18.924
H HOH 99999 -0.571 -0.343 -18.895