我正在使用Windows命令行环境编写一个批处理文件脚本,并希望更改文件中每次出现的一些文本(例如。“FOO”)和另一个(ex。“酒吧”)。最简单的方法是什么?有内置函数吗?
当前回答
下面是我在Win XP上发现的一个解决方案。在我运行的批处理文件中,我包括以下内容:
set value=new_value
:: Setup initial configuration
:: I use && as the delimiter in the file because it should not exist, thereby giving me the whole line
::
echo --> Setting configuration and properties.
for /f "tokens=* delims=&&" %%a in (config\config.txt) do (
call replace.bat "%%a" _KEY_ %value% config\temp.txt
)
del config\config.txt
rename config\temp.txt config.txt
替换后的。bat文件如下所示。我没有找到在同一个批处理文件中包含该函数的方法,因为%%a变量似乎总是给出for循环中的最后一个值。
replace.bat:
@echo off
:: This ensures the parameters are resolved prior to the internal variable
::
SetLocal EnableDelayedExpansion
:: Replaces Key Variables
::
:: Parameters:
:: %1 = Line to search for replacement
:: %2 = Key to replace
:: %3 = Value to replace key with
:: %4 = File in which to write the replacement
::
:: Read in line without the surrounding double quotes (use ~)
::
set line=%~1
:: Write line to specified file, replacing key (%2) with value (%3)
::
echo !line:%2=%3! >> %4
:: Restore delayed expansion
::
EndLocal
其他回答
用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"
dostips.com上的BatchSubstitute.bat是一个使用纯批处理文件进行搜索和替换的示例。
它使用FOR、FIND和CALL SET的组合。
包含“&<>]|^”字符的行可能会被错误处理。
使用FNR
使用fnr实用程序。它比屁有一些优势:
正则表达式 可选的GUI。有一个“生成命令行按钮”,以创建命令行文本放入批处理文件。 多行模式:GUI允许您轻松地使用多行模式。在《放屁》中,你必须手动转行。 允许您选择文本文件编码。也有一个自动检测选项。
下载FNR: http://findandreplace.io/?z=codeplex
使用的例子: fnr——cl——dir "<目录路径>"——fileMask "hibernate。*"——useRegEx——find "find_str_expression"——replace "replace_string"
我用过perl,它的效果非常好。
perl -pi.orig -e "s/<textToReplace>/<textToReplaceWith>/g;" <fileName>
. trans是它将附加到原始文件的扩展名
对于许多匹配的文件,如*.html
for %x in (<filePattern>) do perl -pi.orig -e "s/<textToReplace>/<textToReplaceWith>/g;" %x
这里的很多答案都帮我指明了正确的方向,但是没有一个适合我,所以我把我的解决方案贴出来。
我用的是内置PowerShell的Windows 7。下面是我用来查找/替换文件中所有文本实例的脚本:
powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt"
解释一下:
powershell会启动Windows 7中包含的powershell.exe - command”…是powershell.exe的命令行参数,其中包含要运行的命令 (gc myFile.txt)读取myFile.txt的内容(gc是Get-Content命令的缩写) -replace 'foo', 'bar'简单地运行replace命令将foo替换为bar | Out-File myFile.txt输出到文件myFile.txt -encoding ASCII防止将输出文件转录为unicode,正如注释所指出的那样
Powershell.exe应该已经是PATH语句的一部分,但如果不是,可以添加它。它在我机器上的位置是C:\WINDOWS\system32\WindowsPowerShell\v1.0
显然,现代的windows系统都内置了PowerShell,允许你直接使用
(Get-Content myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt
推荐文章
- 递归复制文件夹,不包括一些文件夹
- 如何使Python脚本在Linux中像服务或守护进程一样运行
- 在批处理文件中处理子字符串的最佳方法是什么?
- 在Sublime Text 2中使用Ctrl+D进行多个选择时,我如何跳过匹配?
- #定义WIN32_LEAN_AND_MEAN具体排除了什么?
- 如何使用文件的行作为命令的参数?
- 如何添加一个设置路径只执行批处理文件?
- 最好的轻量级web服务器(只有静态内容)的Windows
- npm犯错!错误:EPERM:操作不允许,重命名
- 崇高的文本从命令行
- 如何保持Python脚本输出窗口打开?
- 使用.bat文件检查文件夹是否存在
- BAT文件:打开新的cmd窗口并在其中执行命令
- 如何做一个非贪婪匹配在grep?
- 如何查看Windows库(*.lib)的内容