Это тема, которая давно интересовала меня. Мои личные критерии заключаются в том, что решение представляет собой сценарий, который использует только собственные команды Windows и совместим со всеми версиями Windows, начиная с XP.
Я разработал два решения: 1) чисто пакетное решение, которое, как я считаю, настолько эффективно, насколько это возможно для пакетной обработки, и 2) гибридное решение JScript/пакетная обработка, которое является чрезвычайно мощным и при этом очень быстрым.
Я в значительной степени отказался от чистого пакетного решения в пользу гибрида JScript/batch, потому что гибрид более мощный с полной поддержкой регулярных выражений и НАМНОГО быстрее.
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: поиск и замена регулярных выражений для пакетной обработки — легкое редактирование файлов !
Я очень люблю эту утилиту. Большинство пакетных сценариев я делаю как хобби, но я регулярно использую эту утилиту в своей повседневной работе. Он чрезвычайно мощный и быстрый, но требует очень мало кода. Он поддерживает поиск и замену регулярных выражений, но также имеет литеральную опцию /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));
}
}
}
person
dbenham
schedule
13.04.2013
DISABLEDELAYEDEXPANSION
? - person Endoro   schedule 13.04.2013