0

How can i search a string and replace it with a variable. i would like to search version="1.37.0" but the version number could be anything. And there are two "version=" string in package.xml but i would like to replace second one.

how can i search version="x.x.x" and replace it with version="$variable"? is there any one liner?

i did try to use something like this to search:

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

and also same thing for 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
  • No, no one-liner. Please supply a sample that includes your target string, else we could be guessing. What other criteria qualify the choice of the string to be replaced? Commented Aug 1, 2014 at 14:05
  • sorry forgot to mention that, target string could be anything for example 1.1.1, 1.23.2, 1.23.233. search would be hardest thing but replace is just a variable which could have below example versions. Commented Aug 1, 2014 at 14:09

2 Answers 2

1

ad hoc solution, but ...

Edited to adapt to comments

@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)
Sign up to request clarification or add additional context in comments.

6 Comments

@Mihir set "file=%~1" , set "version=%~2" and then call as doChanges.cmd "c:\somewhere\file.xml" "1.2.3.4". Adapt the description changes as you need
thanks again. sorry but is there is any way to save the same file after making changes..
@Mihir, uncoment the last line in the batch
@Mihir, code updated with the changes. Does it work?
how can i check for if exists? if it doesn't exists it adds to the xml file. For example "version=" does not exists in the xml file, so it adds that line with variable.
|
1

Try this :

@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

Very simplist but a good base

Edit :

This will ask for the version value of the second matched 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)

The input file is text.xml and the ouputFile Output.xml

5 Comments

thank you very much for your help, but "x.x.x" could be anything. it could be "1.1.1", "1.2.33". "12.22.222" it is not something which is fixed.
So how can you test it ?
huh? basically i would like a script which could search for "version="1.22.334" and version number could be anything. A user runs a script and also passes a variable which is a new version number.
Ok ! The value of Version is not important ? You just want to change it ?
yup, assuming the value of version is unknown. Sorry for confusion :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.