I'm trying to automate a slightly laborious process which is involved in running computational chemistry calculations with a couple of free pieces of software. I've managed to find answers to most of what I need to do, but I'm missing the last piece of the puzzle, probably because it's quite unusual, and I don't know if it's possible with my chosen language - batch files. I chose this partly because I've had difficulty in the past getting C++ or JavaScript to read or edit other files, and Batch (which was recommended by a friend) did this first time without any difficulty.
I've copied a representative sample of my input file below.
Text above
$DATA
Adenosine
C1 0
O 8.0 0.1319600000 7.8748100000 0.9316500000
DZV 0
C 6.0 -0.5337400000 7.9322600000 2.1608300000
DZV 0
N 7.0 1.4158500000 4.2659800000 1.3661200000
DZV 0
H 1.0 -0.4456400000 8.9556600000 2.5897400000
DZV 0
More coordinates below
I would like to be able to (for example) find the string O 8.0
, and replace the contents of the next line with the contents of a text file. The format,
Atom atomic charge coordinates
text
blank space
needs to remain - the blank space is essential.
An example of the end product is here (for oxygen and carbon; nitrogen and hydrogen have been omitted for brevity):
$DATA
Adenosine
C1 0
O 8.0 0.1319600000 7.8748100000 0.9316500000
S 5
1 2266.1767785 -0.53431809926E-02
2 340.87010191 -0.39890039230E-01
3 77.363135167 -0.17853911985
4 21.479644940 -0.46427684959
5 6.6589433124 -0.44309745172
S 1
1 0.80975975668 1.0000000
S 1
1 0.25530772234 1.0000000
P 3
1 17.721504317 0.43394573193E-01
2 3.8635505440 0.23094120765
3 1.0480920883 0.51375311064
P 1
1 0.27641544411 1.0000000
D 1
1 1.2000000 1.0000000
C 6.0 -0.5337400000 7.9322600000 2.1608300000
S 5
1 1238.4016938 0.54568832082E-02
2 186.29004992 0.40638409211E-01
3 42.251176346 0.18025593888
4 11.676557932 0.46315121755
5 3.5930506482 0.44087173314
S 1
1 0.40245147363 1.0000000
S 1
1 0.13090182668 1.0000000
P 3
1 9.4680970621 0.38387871728E-01
2 2.0103545142 0.21117025112
3 0.54771004707 0.51328172114
P 1
1 0.15268613795 1.0000000
D 1
1 0.8000000 1.0000000
I've managed to successfully do find and replace for strings using the following code:
set "search=SearchItem"
set "replace=Replacement"
set "textfile=text.txt"
set "newfile=newfile.txt"
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
del %textfile%
rename %newfile% %textfile%
I've found "Find a line in a file and replace the next line"-type code here: Find a line in a file and replace the next line But this doesn't quite work - the code seems to turn:
Atom atomic charge coordinates
text
blank space
into:
blank space
replacement text
blank space
And it only does this for one atom.