Replace a substring using Set command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
suitto
Posts: 3
Joined: 25 Feb 2009 18:05

Replace a substring using Set command

#1 Post by suitto » 26 Feb 2009 17:30

Under the section Replace a substring using string substitution,
an example is given as

set str=teh cat in teh hat
echo.%str%
set str=%str:teh=the%
echo.%str%

what if the substring to be replaced contains an equal sign? For instance.
try replace x=2 by y=3

set str=simple equation x=2
echo.%str%
set str=%str:x=2=y=3%
echo.%str%

gives incorrect output.
Any advise please :cry:

_m
Posts: 6
Joined: 27 Jan 2009 10:56

#2 Post by _m » 26 Feb 2009 21:58

Code: Select all

set str=simple equation x=2
echo.%str%
set str=%str:x=y%
set str=%str:2=3%
echo.%str%

suitto
Posts: 3
Joined: 25 Feb 2009 18:05

Replace equal sign using string substitution

#3 Post by suitto » 26 Feb 2009 22:47

Hi _m,
Thanks for your reply.
Your slution works if both x and 2 are unique.

I should be more specific in posing the question
What I am trying to do is actually to search through a large text file,
to find a particular substring say nrel=0,
and change it to nrel=1.
In this case, 0 is not unique, but nrel=0 is unqiue.

Thanks again.

JustJoe
Posts: 6
Joined: 17 Mar 2009 12:54

#4 Post by JustJoe » 17 Mar 2009 13:02

Greetings:

Give this a try

Tested with WinXP Pro

U:\Batch-Variables>modstring "nrel=0" 0 1

Modified: "nrel=0" to: "nrel=1"

==============================================

@echo off

:: modstring.bat
::
:: arg %1 = String to modify
:: arg %2 = String to find
:: arg %3 = Replacement String
::
:: Initialize the local working environment
::
setlocal enableextensions enabledelayedexpansion
set Original=%~1
set Search=%~2
set Replace=%~3

:: The %string:STR1=STR2% function requires a little bit of funky code
:: to make it work when STR1 and STR2 are variables instead of constants.
:: The transition through the call statement is what actually makes it work
:: by expanding the variables properly.
::
:: The calling args are as follows:
::
:: arg1 = Command to execute that modifies the string
:: arg2 = Variable name that will contain the modified string
::
:: arg1 MUST be enclosed in quotes so it is passed as intended
:: arg2 enclosed in quotes for good measure
::
set Magic="set Modified=%%Original:%Search%=%Replace%%%"
call :ModifyString !Magic! "Modified"
echo.
echo Modified: "!Original!" to: !Modified!
echo.

endlocal
goto :End

:: ModifyString
::
:: arg %1 = Magically transformed Command to set the New string in arg %2
:: arg %2 = Target variable of command result
::
:ModifyString
::
:: Initialize the local working environment
setlocal enableextensions enabledelayedexpansion

::
:: Execute the Magically transformed command passed in arg %1 to modify the variable in arg %2
::
%~1

endlocal & set "%~2=%Modified%"
goto :End


:End

==============================================

This method doesn't work in all cases but will be able to do quite a few

cheers

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#5 Post by avery_larry » 19 Mar 2009 10:06

You could search specifically for nrel=0 and THEN replace 0 with 1 -- this would assume there are no other 0's on the same line:


set str=simple equation x=0
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%

Results in simple equation x=0 (No change)

set str=simple equation nrel=0
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%

Results in simple equation nrel=1 (Modified)

set str=simple equation nrel=3
echo.%str%|find /i "nrel=0"
if not errorlevel 1 set str=%str:0=1%
echo.%str%

Results in simple equation nrel=3 (No change)

*SCRIPTER*
Posts: 18
Joined: 16 Mar 2009 08:18
Location: N/A
Contact:

#6 Post by *SCRIPTER* » 19 Mar 2009 15:38

---- :idea:

Post Reply