I need to replace a single line in a file. Generally, this code works fine:
(The actual specifics on what this block is doing is not necessary for this question).
<You could solve this with pure batch!
:readLongLine
< longline.tmp (
for /L %%n in (1 1 20) do set /p part[%%n]=
)
After this your line is splitted into the variables part[1]
.. part[20]
Writing this to a new file you could use
:writeLongLine
<nul (
for /L %%n in (1 1 19) do set /p ".=!part[%%n]!"
(echo !part[20]!)
) > longLine2.tmp
You could use some other scripting language like VBScript, JScript, or PowerShell.
If you want to remain in the batch world, you can use a handy hybrid JScript/batch utility called REPL.BAT that performs regex search and replace on stdin and writes result to stdout. It is quite efficient, and works on any Windows machine from XP onward. It is pure script, so no exe download required. You can get REPL.BAT here. Full documentation is embedded within the script.
Simply use sed, awk or Perl for the job.