0

Without being able to install all sorts of fancy find and replace tools, I need to find and replace strings within text files with the command line from Windows Server 2008.

How would I do that?

Example:

text.md

    Hello world!

Change to:

text.md

    Hello everyone!

I'm looking for something like:

for /f %%A in (text.md) do (

    set "line=%%A"

    if defined line (

        // and here the replacement

    ) ELSE echo.
)
1
  • Plain batch replacement has restrictions and it is useful to know exactly what text we are dealing with - or we could be wasting our time because it won't work. Commented Sep 26, 2013 at 11:19

2 Answers 2

2

Using repl.bat which you would put on the path (say in C:\Windows or a utility folder that you add to the path)

type "text.md"|repl "world" "everyone" >"text.tmp"
move /y "text.tmp" "text.md"

repl.bat is a SED-like helper batch file from - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855

findrepl.bat is GREP-like helper batch file from - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697

If you want plain batch techniques then it will depend on the exact task and text makeup.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, but I'm afraid this goes beyond my understanding. :)
I see. I would need to put repl.bat in the path which I'm afraid I can't do within the environment I'm working with. Thanks for the clear explanation though!
It's most widely useful on the path, but you can also put it in the current directory with your text file and it will work too.
I still am bound tot the fact I cannot do that. :(
You cannot do what? To use repl you copy and paste into notepad, save it as a bat file in the same folder as your text file, and it will work. What further restrictions do you have?
|
1

This has worked for me:

(for /f "tokens=1,* delims=]" %%A in ('"type text.md|find /n /v """') do (

    set "line=%%B"
    if defined line (
        call set "line=echo.%%line:world=everyone%%"
        for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
    ) ELSE echo.

)) >text2.md

move /Y text2.md text.md

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.