2

我想搜索和替换区分大小写的字符串

就像如果我在文本文件中增加 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
4

3 回答 3

8

这是一个让我很感兴趣的话题。我个人的标准是该解决方案是一个仅使用本机 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));
    }
  }
}
于 2013-04-13T13:23:42.730 回答
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 编号来读取每一FINDSTRlinenumber :

删除该前缀并withreplacements逐个字符构建行

  • 查看该行是否以目标replaceme字符串开头
  • 如果它没有删除第一个字符,请将其放在正在构建的字符串的末尾
  • 如果匹配,
    • 附加替换字符串
    • 复制要替换的字符串
    • 删除源字符串的第一个字符和复制到替换,直到复制到替换变为空

并重复直到原件line变空

是的 - 它很慢。但它有效。有点。

欢迎提出改进建议。

于 2013-04-13T10:32:56.893 回答
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

用“New Text”代替“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
于 2013-04-13T18:49:35.557 回答