我想搜索并替换区分大小写的字符串
如果我在文本文件中升起Rise RISE,我只想替换字符串“rise” 下面的代码替换所有三个字符串。请帮帮我!
@Echo on
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION
set file="c:\Users\rawal\Desktop\a\file.txt"
set /p Input=Enter some text:
set OldStr="rise"
set NewStr=%Input%
for /f "tokens=1,* delims=]" %%A in ('"type %file% |find /n /v """') do (
set "line=%%B"
if defined line (
call echo %%line:%OldStr%=%NewStr%%%>> %file%_new
) ELSE echo.
)
move /Y %file%_new %file% > nul
答案 0 :(得分:7)
这是一个让我感兴趣的话题很长一段时间。我的个人标准是该解决方案是一个仅使用本机Windows命令的脚本,并且它与XP以后的所有Windows版本兼容。
我开发了两种解决方案:1)我认为纯批处理解决方案与批处理一样高效,2)混合JScript /批处理解决方案非常强大且速度非常快。
我几乎放弃了纯批处理解决方案,转而支持JScript /批处理混合,因为混合功能更强大,支持完全正则表达式,并且速度更快。
1)纯批量解决方案:MODFILE.BAT
我首先在DOSTIPS上发布了这个:The "ultimate" file search and replace batch utility
批处理功能可以用作独立实用程序,也可以包含在更大的批处理脚本中。
假设该函数是名为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 - Easily edit files!
我真的很喜欢这个实用程序。我做的大多数批处理脚本都是一种爱好,但我在日常工作中经常使用这个实用程序。它非常强大和快速,但只需要很少的代码。它支持正则表达式搜索和替换,但也有/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
选项直接从环境变量读取搜索和替换值,而不是传递字符串文字。 / p>
这是实际的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));
}
}
}
答案 1 :(得分:1)
@ECHO OFF
SETLOCAL
SET "old=rise"
SET "new=deflate"
DEL newfile.txt /F /Q
FOR /f "delims=" %%i IN ('type somefile.txt^|findstr /n "$" ') DO (
ECHO %%i
SET line=%%i
CALL :replace
)
FC somefile.txt newfile.txt
GOTO :eof
:REPLACE
:: first replace all characters up to the colon by nothing
SET line=%line:*:=%
SET "withreplacements="
:loop
IF NOT DEFINED line >>newfile.txt ECHO(%withreplacements%&GOTO :EOF
ECHO %line%|FINDSTR /b /l /c:"%old%" >NUL
IF ERRORLEVEL 1 SET withreplacements=%withreplacements%%line:~0,1%&SET line=%line:~1%&GOTO loop
SET withreplacements=%withreplacements%%new%
SET remove=%old%
:loploop
IF DEFINED remove SET remove=%remove:~1%&SET line=%line:~1%&GOTO loploop
GOTO loop
这是一个相对简单的方法。它对某些字符有明显的敏感性,"^&|<>
是问题 - 也许是其他一些问题 - 但space,;%!)(
似乎很好。
它通过编号usinf FINDSTR
读取每行,将linenumber :
放在每行的开头
删除该前缀,并逐个字符地构建withreplacements
行
replaceme
字符串并重复,直到原始line
变空
是的 - 它是S-L-O-W。但它的确有效。有点。
欢迎改进建议。
答案 2 :(得分:0)
我们都知道批处理文件有多个限制,因此难以创建通用解决方案。因此,我总是首先尝试满足某个特定问题的特定要求。如果这是可能的,那么Batch为其他类似问题提供更通用的解决方案的限制当前没有被某人请求并不重要,对吗?
下面的批处理文件对一个字符串进行区分大小写的替换另一个字符串非常快,但它在包含原始字符串的行中失败,这些字符串在不同的大小写组合中写入,包括目标组合。我认为这种方法足以满足大多数有这种要求的用户。
@echo off
setlocal EnableDelayedExpansion
set /P "file=Enter file name: "
set /P "OldStr=Enter original text: "
set /P "NewStr=Enter new text: "
rem Get list of numbers of matching lines to replace
set n=0
for /F "delims=:" %%a in ('findstr /N /C:"%OldStr%" "%file%"') do (
set /A n+=1
set replace[!n!]=%%a
)
if %n% equ 0 (
echo Original text not found in file
goto :EOF
)
set /A n+=1
set replace[%n%]=0
rem Process all lines in the file
setlocal DisableDelayedExpansion
set i=1
(for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "%file%"') do (
set line=
set "line=%%b"
setlocal EnableDelayedExpansion
rem If this line have the original string...
for %%i in (!i!) do if %%a equ !replace[%%i]! (
rem ... replace it and advance to next matching line number
echo !line:%OldStr%=%NewStr%!
endlocal & set /A i=%%i+1
) else (
echo(!line!
endlocal
)
)) > "%file%_new.txt
rem If you want to replace the original file, remove REM from next line:
REM move /Y "%file%_new.txt" "%file%"
例如,此输入文件:
This line is not changed: Rise.
No problem with special characters: & | < > ! " ^
This line is changed: rise
This line is not changed: RISE
This line is incorrectly changed: Rise & rise
用“新文本”替换“上升”,产生:
This line is not changed: Rise.
No problem with special characters: & | < > ! " ^
This line is changed: New Text
This line is not changed: RISE
This line is incorrectly changed: New Text & New Text