如何使用Windows命令行环境查找和替换文件中的文本?

时间:2020-03-05 18:52:33  来源:igfitidea点击:

我正在使用Windows命令行环境编写批处理文件脚本,并希望将文件(例如" FOO")中的某些文本每次更改为另一个文件(例如" BAR")。最简单的方法是什么?有内置功能吗?

解决方案

回答

我认为没有任何方法可以使用任何内置命令。我建议我们下载Gnuwin32或者UnxUtils之类的东西并使用sed命令(或者仅下载sed):

sed -c s/FOO/BAR/g filename

回答

如果我们使用的是支持.Net 2.0的Windows版本,那么我将替换Shell。 PowerShell从命令行为我们提供了.Net的全部功能。也有许多内置的命令行开关。下面的示例将解决问题。我使用的是命令的全名,别名较短,但这为Google提供了一些便利。

(Get-Content test.txt) | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt

回答

下载Cygwin(免费)并在Windows命令行中使用类似Unix的命令。

最好的选择:sed

回答

dostips.com上的" BatchSubstitute.bat"是使用纯批处理文件进行搜索和替换的示例。

它使用了FOR,FIND和CALL SET的组合。

在""&<>] | ^`之间包含字符的行可能会被错误地对待。

回答

这是批处理脚本不能很好完成的一件事。

链接到morechilli的脚本可用于某些文件,但不幸的是,它将被包含管道和&字符等字符的文件阻塞。

VBScript是用于此任务的更好的内置工具。有关示例,请参见本文:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx

回答

看看是否有cmd.exe的sed类实用程序在Windows下要求等效的sed,也应适用于此问题。执行摘要:

  • 可以在批处理文件中完成,但这不是很漂亮
  • 如果我们可以安装或者仅复制一个exe文件,则有很多可用的第三方可执行文件将为我们完成此操作
  • 如果我们需要能够在Windows框上运行而无需修改等功能,则可以使用VBScript或者类似工具来完成。

回答

可能有点晚了,但是我经常在寻找类似的东西,因为我不想摆脱获得软件批准的痛苦。

但是,我们通常以各种形式使用FOR语句。有人创建了一个有用的批处理文件来进行搜索和替换。在这里看看。重要的是要了解所提供的批处理文件的限制。因此,我不会在此答案中复制源代码。

回答

刚使用过的FART(" Find和R eplace T ext"命令行实用程序):
优秀的小型免费软件,可在大量文件中进行文本替换。

设置文件位于SourceForge上。

用法示例:

fart.exe -p -r -c -- C:\tools\perl-5.8.9\* @@APP_DIR@@ C:\tools

将预览该替换,以在此Perl发行版的文件中以递归方式进行。

唯一的问题:FART网站的图标并不完全是高雅,精致或者优雅的;)

2017年更新(7年后),jagb在Mikail Tun的2011年文章"放屁寻找和替换文本的方式"中的评论中指出

回答

这是我在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

replace.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

回答

Power Shell命令的工作原理很像

(
test.txt | ForEach-Object { $_ -replace "foo", "bar" } | Set-Content test2.txt
)

回答

创建文件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 replace.vbs" C:\ Scripts \ Text.txt""吉姆""詹姆斯"

回答

我用过perl,而且效果很好。

perl -pi.orig -e "s/<textToReplace>/<textToReplaceWith>/g;" <fileName>

.orig是它将添加到原始文件的扩展名

对于许多匹配的文件,例如* .html

for %x in (<filePattern>) do perl -pi.orig -e "s/<textToReplace>/<textToReplaceWith>/g;" %x