使用Windows命令行替换文本文件中的文本

时间:2013-09-25 13:32:01

标签: command-line

无法安装各种花哨的查找和替换工具,我需要使用Windows Server 2008中的命令行查找和替换文本文件中的字符串。

我该怎么做?

示例:

text.md

    Hello world!

更改为:

text.md

    Hello everyone!

我正在寻找类似的东西:

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

    set "line=%%A"

    if defined line (

        // and here the replacement

    ) ELSE echo.
)

2 个答案:

答案 0 :(得分:0)

使用您将放在路径上的repl.bat(例如在C:\ Windows或您添加到路径中的实用程序文件夹)

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

repl.bat是来自 - http://www.dostips.com/forum/viewtopic.php?f=3&t=3855

的类似SED的帮助程序批处理文件

findrepl.bat是来自 - http://www.dostips.com/forum/viewtopic.php?f=3&t=4697

的类似GREP的帮助程序批处理文件

如果您想要简单的批处理技术,那么它将取决于确切的任务和文本构成。

答案 1 :(得分:0)

这对我有用:

(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