这是一个让我很感兴趣的话题。我个人的标准是该解决方案是一个仅使用本机 Windows 命令的脚本,并且它与从 XP 开始的所有 Windows 版本兼容。
我已经开发了两种解决方案:1) 我认为对于批处理而言尽可能高效的纯批处理解决方案,以及 2) 非常强大且速度非常快的混合 JScript/批处理解决方案。
我几乎放弃了纯批处理解决方案,转而使用 JScript/批处理混合,因为混合功能更强大,完全支持正则表达式,而且速度更快。
1)纯批处理解决方案:MODFILE.BAT
我首先在 DOSTIPS 上发表了这篇文章:“终极”文件搜索和替换批处理实用程序
批处理功能可用作独立实用程序,或合并到更大的批处理脚本中。
假设该函数是一个名为 MODFILE.BAT 的文件中的独立实用程序,该文件位于您当前的文件夹中,或者位于您的 PATH 中的某个位置,那么您的脚本将变为:
@echo off
setlocal enableDelayedExpansion
set file="c:\Users\rawal\Desktop\a\file.txt"
set "OldStr=rise"
set "NewStr="
set /p "NewStr=Enter some text: "
call ModFile "%file%" OldStr NewStr
这是 ModFile 函数本身。完整的文档嵌入在脚本中。我非常努力地优化代码,并消除困扰大多数批处理解决方案的限制。但是文档中列出了一些剩余的限制。
@echo off
:modFile File SearchVar [ReplaceVar] [/I]
::
:: Perform a search and replace operation on each line within File.
::
:: SearchVar = A variable containing the search string.
::
:: ReplaceVar = A variable containing the replacement string.
:: If ReplaceVar is missing or is not defined then the
:: search string is replaced with an empty string.
::
:: The /I option specifies a case insensitive search.
::
:: A backup of the original File is made with an extension of .bak
:: prior to making any changes.
::
:: The number of replacements made is returned as errorlevel.
::
:: If an error occurs then no changes are made and
:: the errorlevel is set to -1.
::
:: Limitations
:: - File must use Windows style line terminators <CR><LF>.
:: - Trailing control characters will be stripped from each line.
:: - The maximum input line length is 1021 characters.
::
setlocal enableDelayedExpansion
::error checking
if "%~2"=="" (
>&2 echo ERROR: Insufficient arguments
exit /b -1
)
if not exist "%~1" (
>&2 echo ERROR: Input file "%~1" does not exist
exit /b -1
)
2>nul pushd "%~1" && (
popd
>&2 echo ERROR: Input file "%~1" does not exist
exit /b -1
)
if not defined %~2 (
>&2 echo ERROR: searchVar %2 not defined
exit /b -1
)
if /i "%~3"=="/I" (
>&2 echo ERROR: /I option can only be specified as 4th argument
exit /b -1
)
if "%~4" neq "" if /i "%~4" neq "/I" (
>&2 echo ERROR: Invalid option %4
exit /b -1
)
::get search and replace strings
set "_search=!%~2!"
set "_replace=!%~3!"
::build list of lines that must be changed, simply exit if none
set "replaceCnt=0"
set changes="%temp%\modFileChanges%random%.tmp"
<"%~1" find /n %~4 "!_search:"=""!^" >%changes% || goto :cleanup
::compute length of _search
set "str=A!_search!"
set searchLen=0
for /l %%A in (12,-1,0) do (
set /a "searchLen|=1<<%%A"
for %%B in (!searchLen!) do if "!str:~%%B,1!"=="" set /a "searchLen&=~1<<%%A"
)
::count number of lines + 1
for /f %%N in ('find /v /c "" ^<"%~1"') do set /a lnCnt=%%N+1
::backup source file
if exist "%~1.bak" del "%~1.bak"
ren "%~1" "%~nx1.bak"
::initialize
set "skip=2"
<"%~1.bak" (
%=for each line that needs changing=%
for %%l in (!searchLen!) do for /f "usebackq delims=[]" %%L in (%changes%) do (
%=read and write preceding lines that don't need changing=%
for /l %%N in (!skip! 1 %%L) do (
set "ln="
set /p "ln="
if defined ln if "!ln:~1021!" neq "" goto :lineLengthError
echo(!ln!
)
%=read the line that needs changing=%
set /p "ln="
if defined ln if "!ln:~1021!" neq "" goto :lineLengthError
%=compute length of line=%
set "str=A!ln!"
set lnLen=0
for /l %%A in (12,-1,0) do (
set /a "lnLen|=1<<%%A"
for %%B in (!lnLen!) do if "!str:~%%B,1!"=="" set /a "lnLen&=~1<<%%A"
)
%=perform search and replace on line=%
set "modLn="
set /a "end=lnLen-searchLen, beg=0"
for /l %%o in (0 1 !end!) do (
if %%o geq !beg! if %~4 "!ln:~%%o,%%l!"=="!_search!" (
set /a "len=%%o-beg"
for /f "tokens=1,2" %%a in ("!beg! !len!") do set "modLn=!modLn!!ln:~%%a,%%b!!_replace!"
set /a "beg=%%o+searchLen, replaceCnt+=1"
)
)
for %%a in (!beg!) do set "modLn=!modLn!!ln:~%%a!"
%=write the modified line=%
echo(!modLn!
%=prepare for next iteration=%
set /a skip=%%L+2
)
%=read and write remaining lines that don't need changing=%
for /l %%N in (!skip! 1 !lnCnt!) do (
set "ln="
set /p "ln="
if defined ln if "!ln:~1021!" neq "" goto :lineLengthError
echo(!ln!
)
) >"%~1"
:cleanup
del %changes%
exit /b %replaceCnt%
:lineLengthError
del %changes%
del "%~1"
ren "%~nx1.bak" "%~1"
>&2 echo ERROR: Maximum input line length exceeded. Changes aborted.
exit /b -1
2) 混合 JScript/批处理解决方案:REPL.BAT
我第一次在 DOSTIPS 上发表了这篇文章:regex search and replace for batch - 轻松编辑文件!
我真的很喜欢这个实用程序。大多数批处理脚本都是我的业余爱好,但我在日常工作中经常使用这个实用程序。它非常强大和快速,但只需要很少的代码。它支持正则表达式搜索和替换,但也有/L
文字选项。默认情况下,搜索区分大小写。
假设 REPL.BAT 在您当前的文件夹中,或者在您的 PATH 中的某个位置,那么您的代码将变为:
@echo off
setlocal enableDelayedExpansion
set "file=c:\Users\rawal\Desktop\a\file.txt"
set "OldStr=rise"
set "NewStr="
set /p "NewStr=Enter some text: "
type "%file%" | repl OldStr NewStr VL >"%file%.new"
move /y "%file%.new" "%file%" >nul
我使用L
选项来强制进行文字搜索而不是默认的正则表达式搜索,以及V
直接从环境变量中读取搜索和替换值而不是传递字符串文字的选项。
这是实际的 REPL.BAT 实用程序。完整的文档嵌入在脚本中。
@if (@X)==(@Y) @end /* Harmless hybrid line that begins a JScript comment
::************ Documentation ***********
:::
:::REPL Search Replace [Options [SourceVar]]
:::REPL /?
:::
::: Performs a global search and replace operation on each line of input from
::: stdin and prints the result to stdout.
:::
::: Each parameter may be optionally enclosed by double quotes. The double
::: quotes are not considered part of the argument. The quotes are required
::: if the parameter contains a batch token delimiter like space, tab, comma,
::: semicolon. The quotes should also be used if the argument contains a
::: batch special character like &, |, etc. so that the special character
::: does not need to be escaped with ^.
:::
::: If called with a single argument of /? then prints help documentation
::: to stdout.
:::
::: Search - By default this is a case sensitive JScript (ECMA) regular
::: expression expressed as a string.
:::
::: JScript regex syntax documentation is available at
::: http://msdn.microsoft.com/en-us/library/ae5bf541(v=vs.80).aspx
:::
::: Replace - By default this is the string to be used as a replacement for
::: each found search expression. Full support is provided for
::: substituion patterns available to the JScript replace method.
::: A $ literal can be escaped as $$. An empty replacement string
::: must be represented as "".
:::
::: Replace substitution pattern syntax is documented at
::: http://msdn.microsoft.com/en-US/library/efy6s3e6(v=vs.80).aspx
:::
::: Options - An optional string of characters used to alter the behavior
::: of REPL. The option characters are case insensitive, and may
::: appear in any order.
:::
::: I - Makes the search case-insensitive.
:::
::: L - The Search is treated as a string literal instead of a
::: regular expression. Also, all $ found in Replace are
::: treated as $ literals.
:::
::: B - The Search must match the beginning of a line.
::: Mostly used with literal searches.
:::
::: E - The Search must match the end of a line.
::: Mostly used with literal searches.
:::
::: V - Search and Replace represent the name of environment
::: variables that contain the respective values. An undefined
::: variable is treated as an empty string.
:::
::: M - Multi-line mode. The entire contents of stdin is read and
::: processed in one pass instead of line by line. ^ anchors
::: the beginning of a line and $ anchors the end of a line.
:::
::: X - Enables extended substitution pattern syntax with support
::: for the following escape sequences:
:::
::: \\ - Backslash
::: \b - Backspace
::: \f - Formfeed
::: \n - Newline
::: \r - Carriage Return
::: \t - Horizontal Tab
::: \v - Vertical Tab
::: \xnn - Ascii (Latin 1) character expressed as 2 hex digits
::: \unnnn - Unicode character expressed as 4 hex digits
:::
::: Escape sequences are supported even when the L option is used.
:::
::: S - The source is read from an environment variable instead of
::: from stdin. The name of the source environment variable is
::: specified in the next argument after the option string.
:::
::************ Batch portion ***********
@echo off
if .%2 equ . (
if "%~1" equ "/?" (
findstr "^:::" "%~f0" | cscript //E:JScript //nologo "%~f0" "^:::" ""
exit /b 0
) else (
call :err "Insufficient arguments"
exit /b 1
)
)
echo(%~3|findstr /i "[^SMILEBVX]" >nul && (
call :err "Invalid option(s)"
exit /b 1
)
cscript //E:JScript //nologo "%~f0" %*
exit /b 0
:err
>&2 echo ERROR: %~1. Use REPL /? to get help.
exit /b
************* JScript portion **********/
var env=WScript.CreateObject("WScript.Shell").Environment("Process");
var args=WScript.Arguments;
var search=args.Item(0);
var replace=args.Item(1);
var options="g";
if (args.length>2) {
options+=args.Item(2).toLowerCase();
}
var multi=(options.indexOf("m")>=0);
var srcVar=(options.indexOf("s")>=0);
if (srcVar) {
options=options.replace(/s/g,"");
}
if (options.indexOf("v")>=0) {
options=options.replace(/v/g,"");
search=env(search);
replace=env(replace);
}
if (options.indexOf("l")>=0) {
options=options.replace(/l/g,"");
search=search.replace(/([.^$*+?()[{\\|])/g,"\\$1");
replace=replace.replace(/\$/g,"$$$$");
}
if (options.indexOf("b")>=0) {
options=options.replace(/b/g,"");
search="^"+search
}
if (options.indexOf("e")>=0) {
options=options.replace(/e/g,"");
search=search+"$"
}
if (options.indexOf("x")>=0) {
options=options.replace(/x/g,"");
replace=replace.replace(/\\\\/g,"\\B");
replace=replace.replace(/\\b/g,"\b");
replace=replace.replace(/\\f/g,"\f");
replace=replace.replace(/\\n/g,"\n");
replace=replace.replace(/\\r/g,"\r");
replace=replace.replace(/\\t/g,"\t");
replace=replace.replace(/\\v/g,"\v");
replace=replace.replace(/\\x[0-9a-fA-F]{2}|\\u[0-9a-fA-F]{4}/g,
function($0,$1,$2){
return String.fromCharCode(parseInt("0x"+$0.substring(2)));
}
);
replace=replace.replace(/\\B/g,"\\");
}
var search=new RegExp(search,options);
if (srcVar) {
WScript.Stdout.Write(env(args.Item(3)).replace(search,replace));
} else {
while (!WScript.StdIn.AtEndOfStream) {
if (multi) {
WScript.Stdout.Write(WScript.StdIn.ReadAll().replace(search,replace));
} else {
WScript.Stdout.WriteLine(WScript.StdIn.ReadLine().replace(search,replace));
}
}
}