Page 1 of 1

Replace file lines: if statement and goto

Posted: 17 Jun 2024 10:46
by Wrenna
Hello,

I have a file structured in this way:
\begin{flushleft}
The quick brown fox jumps over the lazy dog
\end{flushleft}
\raggedright
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
What I would like to achieve is replace the lines with \begin{flushleft}, \end{flushleft} and \raggedright with strings of my choice.
It is therefore necessary for me to rewrite the file.
Since batch doesn't support more than three statements at a time (if, else if and else), I need a script that determines if the line is the one to replace and replaces it, otherwise write the line intact.
The only way is to the call function. But how do I pass the line under consideration as an argument?

Code: Select all

@echo off 

:Variables
set InputFile=merged.tex
set OutputFile=merged-new.tex

:Replace
>"%OutputFile%" (
for /f "usebackq delims=" %%A in ("%InputFile%") do (
    call :check %%A
	)
)

:check
::echo replace or intacted line
exit /b 0

MOVE %OutputFile% %InputFile%

Re: Replace file lines: if statement and goto

Posted: 17 Jun 2024 13:34
by Squashman
Oddly familiar question that I posted two possible solutions to on StackOverFlow yesterday.
https://stackoverflow.com/q/78630421/1417694

Re: Replace file lines: if statement and goto

Posted: 17 Jun 2024 17:21
by Aacini
I wrote a new solution for this problem that allows a simple (and fast!) processing of a large number of replacements. I posted my solution in the original SO forum (because I can earn some rep points there)...

Antonio

Re: Replace file lines: if statement and goto

Posted: 18 Jun 2024 04:26
by miskox
Aacini's solutions always impress me. This is one of them: viewtopic.php?f=3&t=10579#p67850

Saso

Re: Replace file lines: if statement and goto

Posted: 18 Jun 2024 07:56
by Squashman
Aacini wrote:
17 Jun 2024 17:21
I wrote a new solution for this problem that allows a simple (and fast!) processing of a large number of replacements. I posted my solution in the original SO forum (because I can earn some rep points there)...

Antonio
Nice job. I do like your logic.