使用批处理命令搜索和替换字符串

时间:2014-08-01 14:01:39

标签: batch-file search replace

如何搜索字符串并将其替换为变量。 我想搜索version =“1.37.0”,但版本号可能是任何东西。在package.xml中有两个“version =”字符串,但我想替换第二个。

如何搜索version =“x.x.x”并将其替换为version =“$ variable”? 有没有一个班轮?

我确实试图用这样的东西来搜索:

findstr "version="[0-9].[0-9].[0-9]" package.xml

对于desrciption也是同样的事情=“$ variable1”

package.xml中

<?xml version="1.0" encoding="utf-8"?>
<PackageManifest>
  <Package name="audio"
           description="something.  . .."
           version="1.37.0"
           comment="">
   </Package>
</PackageManifest>

2 个答案:

答案 0 :(得分:1)

试试这个:

@echo off

set $FindStr=Version="x.x.x"
set $ReplString=Version="y.y.y"

setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type test.xml') do (
   set $Ver=%%a
   set $Ver=!$Ver: =!
   If /i !$Ver!==%$FindStr% set $Ver=%$ReplString%
   echo !$Ver! ) >> Output.xml

非常简单,但基础很好

修改:

这将询问第二个匹配的version=

的版本值
@echo off

set "$FindStr=Version="
set $c=1
setlocal enabledelayedexpansion
for /f "delims=" %%a in ('type test.xml') do (
   set $Ver=%%a
   set $Ver=!$Ver: =!
   If /i "!$Ver:~0,8!"=="%$FindStr%" (
        if  !$c! GTR 1 (
             set /p "$NewVer=Enter New version : "
             set $Ver=%$FindStr%!$NewVer!)
        set /a $c+=1)
   echo !$Ver! >> Output.xml)

输入文件为text.xml,ouputFile为Output.xml

答案 1 :(得分:1)

临时解决方案,但......

已编辑以适应评论

@echo off

    setlocal enableextensions disabledelayedexpansion

    rem Check input parameters. Needed the input file, the version and the description
    if "%~3"=="" goto :eof

    set "file=%~1"
    set "newVersion=%~2"
    set "newDescription=%~3"

    rem Read the file into memory
    for /f "tokens=1,* delims=:" %%a in ('findstr /n "^" "%file%"') do (
        set /a "line=1000+%%a*10"
        setlocal enabledelayedexpansion
        for %%c in (!line!) do endlocal & set "l_%%c=|%%b"
    )

    rem %%a will search the required lines
    rem %%c remove blanks at the start of the line
    rem %%d get the key name

    for /f "tokens=2,* delims=_=|" %%a in (
        'set l_1 ^| findstr /i /r /c:"^[^<]*version=" /c:"description=" /c:"^[^<]*<Package"'
    ) do for /f %%c in ("%%b") do for /f "delims==" %%d in ("%%c") do (

        if /i "%%d"=="description"     ( set "value=%newDescription%" & set "newDescription="
        ) else if /i "%%d"=="version"  ( set "value=%newVersion%"     & set "newVersion="
        ) else if /i "%%d"=="<Package" ( set "packageLine=%%a"        & set "value="
        ) else set "value="

        if defined value ( setlocal enabledelayedexpansion
            for /f "delims=" %%z in ("!value!") do ( endlocal 
                for /f tokens^=1^,2^,^*^ delims^=^" %%e in ("%%b") do set "l_%%a=|%%e"%%z"%%g"
            )
        )
    )

    rem Include the missing values
    set /a "packageLine+=1"
    if defined newDescription set "l_%packageLine%=|           description="%newDescription%""
    set /a "packageLine+=1"
    if defined newVersion set "l_%packageLine%=|           version="%newVersion%""


    rem Output the changed information to console
    for /f "tokens=1,* delims=|" %%a in ('set l_1') do echo(%%b

    rem Save to file
    >"%file%" (for /f "tokens=1,* delims=|" %%a in ('set l_1') do echo(%%b)