我正在使用Windows命令行环境编写一个批处理文件脚本,并希望更改文件中每次出现的一些文本(例如。“FOO”)和另一个(ex。“酒吧”)。最简单的方法是什么?有内置函数吗?
当前回答
我知道我迟到了。
就我个人而言,我喜欢以下的解决方案: ——http://www.dostips.com/DtTipsStringManipulation.php Snippets.Replace
我们还广泛使用重复数据删除功能,帮助我们每天通过SMTP发送大约500封电子邮件: ——https://groups.google.com/forum/ # ! / alt.msdos.batch.nt / sj8IUhMOq6o话题
这些都是本地工作,不需要额外的工具或实用程序。
替代者:
DEL New.txt
setLocal EnableDelayedExpansion
For /f "tokens=* delims= " %%a in (OLD.txt) do (
Set str=%%a
set str=!str:FOO=BAR!
echo !str!>>New.txt
)
ENDLOCAL
DEDUPLICATOR(注意使用-9作为ABA编号):
REM DE-DUPLICATE THE Mapping.txt FILE
REM THE DE-DUPLICATED FILE IS STORED AS new.txt
set MapFile=Mapping.txt
set ReplaceFile=New.txt
del %ReplaceFile%
::DelDupeText.bat
rem https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/sj8IUhMOq6o
setLocal EnableDelayedExpansion
for /f "tokens=1,2 delims=," %%a in (%MapFile%) do (
set str=%%a
rem Ref: http://www.dostips.com/DtTipsStringManipulation.php#Snippets.RightString
set str=!str:~-9!
set str2=%%a
set str3=%%a,%%b
find /i ^"!str!^" %MapFile%
find /i ^"!str!^" %ReplaceFile%
if errorlevel 1 echo !str3!>>%ReplaceFile%
)
ENDLOCAL
谢谢!
其他回答
这里的很多答案都帮我指明了正确的方向,但是没有一个适合我,所以我把我的解决方案贴出来。
我用的是内置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
用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"
我用过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
还可以在https://zoomicon.github.io/tranXform/上看到Replace和ReplaceFilter工具(包括源代码)。第二个是过滤器。
替换文件字符串的工具在VBScript中(需要Windows脚本主机[WSH]才能在旧的Windows版本中运行)
除非你用最新的Delphi(或FreePascal/Lazarus)重新编译,否则过滤器可能无法使用Unicode。
我知道我迟到了。
就我个人而言,我喜欢以下的解决方案: ——http://www.dostips.com/DtTipsStringManipulation.php Snippets.Replace
我们还广泛使用重复数据删除功能,帮助我们每天通过SMTP发送大约500封电子邮件: ——https://groups.google.com/forum/ # ! / alt.msdos.batch.nt / sj8IUhMOq6o话题
这些都是本地工作,不需要额外的工具或实用程序。
替代者:
DEL New.txt
setLocal EnableDelayedExpansion
For /f "tokens=* delims= " %%a in (OLD.txt) do (
Set str=%%a
set str=!str:FOO=BAR!
echo !str!>>New.txt
)
ENDLOCAL
DEDUPLICATOR(注意使用-9作为ABA编号):
REM DE-DUPLICATE THE Mapping.txt FILE
REM THE DE-DUPLICATED FILE IS STORED AS new.txt
set MapFile=Mapping.txt
set ReplaceFile=New.txt
del %ReplaceFile%
::DelDupeText.bat
rem https://groups.google.com/forum/#!topic/alt.msdos.batch.nt/sj8IUhMOq6o
setLocal EnableDelayedExpansion
for /f "tokens=1,2 delims=," %%a in (%MapFile%) do (
set str=%%a
rem Ref: http://www.dostips.com/DtTipsStringManipulation.php#Snippets.RightString
set str=!str:~-9!
set str2=%%a
set str3=%%a,%%b
find /i ^"!str!^" %MapFile%
find /i ^"!str!^" %ReplaceFile%
if errorlevel 1 echo !str3!>>%ReplaceFile%
)
ENDLOCAL
谢谢!
推荐文章
- 如何从终端/命令行调用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中跨本地文件系统回购