我正在使用Windows命令行环境编写一个批处理文件脚本,并希望更改文件中每次出现的一些文本(例如。“FOO”)和另一个(ex。“酒吧”)。最简单的方法是什么?有内置函数吗?
当前回答
我是Aba搜索和替换的作者,你可以从Windows命令行使用。它可以在没有任何用户交互的情况下进行批量替换,也可以替换多个文件中的文本,而不仅仅是一个文件。
欢迎您试用我的工具;我很乐意回答任何问题。
其他回答
Replace—使用字符串替换替换子字符串 描述:使用字符串替换特性将子字符串替换为另一个字符串。这里显示的示例将字符串变量str中所有出现的“teh”拼写错误替换为“The”。
set str=teh cat in teh hat
echo.%str%
set str=%str:teh=the%
echo.%str%
脚本输出:
teh cat in teh hat
the cat in the hat
裁判:http://www.dostips.com/DtTipsStringManipulation.php # Snippets.Replace
用replace .bat
1)用e?选项,将计算特殊字符序列,如\n\r和unicode序列。在这种情况下,将替换引用的“Foo”和“Bar”:
call replacer.bat "e?C:\content.txt" "\u0022Foo\u0022" "\u0022Bar\u0022"
2)直接替换没有引用的Foo和Bar。
call replacer.bat "C:\content.txt" "Foo" "Bar"
还可以在https://zoomicon.github.io/tranXform/上看到Replace和ReplaceFilter工具(包括源代码)。第二个是过滤器。
替换文件字符串的工具在VBScript中(需要Windows脚本主机[WSH]才能在旧的Windows版本中运行)
除非你用最新的Delphi(或FreePascal/Lazarus)重新编译,否则过滤器可能无法使用Unicode。
创建文件replace.vbs:
Const ForReading = 1
Const ForWriting = 2
strFileName = Wscript.Arguments(0)
strOldText = Wscript.Arguments(1)
strNewText = Wscript.Arguments(2)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFileName, ForReading)
strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, strOldText, strNewText)
Set objFile = objFSO.OpenTextFile(strFileName, ForWriting)
objFile.Write strNewText 'WriteLine adds extra CR/LF
objFile.Close
要使用这个修改后的脚本(我们称之为replace.vbs),只需在命令提示符中键入类似于下面的命令:
cscript替换。vbs "C:\Scripts\Text.txt" "Jim " "James "
Powershell命令-
获取文件的内容,并将其替换为其他文本,然后存储到另一个文件中
命令1 | ForEach-Object {$_.replace("some_text","replace_text").replace("some_other_text","replace_text")} | Set-Content filename2.xml
将另一个文件复制到原始文件中
Command2
复制-项目-路径filename2.xml -目标文件名.xml -PassThru .xml
删除另一个文件
命令3
Remove-Item filename2.xml
推荐文章
- 如何从终端/命令行调用VS代码编辑器
- 在Jar文件中运行类
- 我如何找到哪个程序正在使用端口80在Windows?
- 在Windows中有像GREP这样的模式匹配实用程序吗?
- 如何在Windows命令提示符下运行.sh ?
- 如何从命令行通过mysql运行一个查询?
- 如何从命令行在windows中找到mysql数据目录
- 如何从命令行安装cygwin组件?
- 如何更改Git日志日期格式
- 在没有事件源注册的情况下写入Windows应用程序事件日志
- Windows FINDSTR命令的未记录的特性和限制是什么?
- 无法在Windows上从/usr/local/ssl/openssl.cnf加载配置信息
- 使iTerm以与其他操作系统相同的方式翻译“元键”
- 用Windows任务调度器运行批处理文件
- GIT克隆在windows中跨本地文件系统回购