Command line 用Windows命令行替换文本文件中的文本
由于无法安装各种奇特的查找和替换工具,我需要使用Windows Server 2008中的命令行查找和替换文本文件中的字符串 我该怎么做 例如:Command line 用Windows命令行替换文本文件中的文本,command-line,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 replacemen
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.
)
使用
repl.bat
将其放在路径上(例如在C:\Windows或添加到路径的实用程序文件夹中)
repl.bat
是一个类似于SED的助手批处理文件,来自-
findrepl.bat
是类似于GREP的助手批处理文件,来自-
如果您想要纯批量技术,那么它将取决于确切的任务和文本组成。这对我来说很有效:
(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
谢谢,但我恐怕这超出了我的理解我懂了。我需要将
repl.bat
放在我恐怕无法在我工作的环境中执行的路径中。谢谢你的解释!它在路径上非常有用,但您也可以将它与文本文件一起放在当前目录中,它也会工作。我仍然会意识到我不能这样做(您不能做什么?若要使用repl,请复制并粘贴到记事本中,将其作为bat文件保存在与文本文件相同的文件夹中,然后它就会工作。您还有什么其他限制?纯批量替换有限制,了解我们正在处理的文本非常有用-或者我们可能会因为无法工作而浪费时间。
(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