How to search and replace a string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

How to search and replace a string

#1 Post by timbertuck » 21 Dec 2011 14:35

i have an environment variable that has the value of: random%random%random and i need to replace it with random~random~random

i tried using set like so
set envr1=%envr1:%=~%
which didnt work (altered the string to %envar1:%=~%), so i tried escaping it (using ^, is that right?)
set envr1=%envr1:^%=~% but that didn't alter the string at all

so is this the best way to replace it (as an env var) or maybe echo and redirect it to a file and to the search and replace there..

any ideas?

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to search and replace a string

#2 Post by aGerman » 21 Dec 2011 15:01

Delayed variable expansion may help.

Code: Select all

@echo off
set "envr1=random%%random%%random"

echo %envr1%

setlocal enabledelayedexpansion
for /f "delims=" %%i in ("!envr1:%%=~!") do (endlocal &set "envr1=%%i")

echo %envr1%

pause

Regards
aGerman

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: How to search and replace a string

#3 Post by timbertuck » 22 Dec 2011 13:14

aGerman wrote:Delayed variable expansion may help.

Code: Select all

@echo off
set "envr1=random%%random%%random"
echo %envr1%
setlocal enabledelayedexpansion
for /f "delims=" %%i in ("!envr1:%%=~!") do (endlocal &set "envr1=%%i")
echo %envr1%
pause

Regards
aGerman


many thanks aGerman, Im trying this now. i appreciate the help

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: How to search and replace a string

#4 Post by timbertuck » 29 Dec 2011 11:26

i have the following:

set envar1=val%val%val%val
echo envar1 > filename

if i pipe it to a file, then the find command like so:

find /c "%" filename

will come back with ------ filename: #,
where # is a number counting how many lines contain the percentage sign

findstr apparently does not count the occurence either,

so how best to count (on one line) the amount of percentage signs (it should come back with 3 in the example)?

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to search and replace a string

#5 Post by aGerman » 29 Dec 2011 11:57

Try that code:

Code: Select all

@echo off
set "envar1=val%%val%%val%%val"
echo %envar1%

setlocal EnableDelayedExpansion
set "str1=A!Envar1!"
set "str2=A!Envar1:%%=!"
set /a "originLen=0, erasedLen=0"
for /l %%A in (12,-1,0) do (
  set /a "originLen|=1<<%%A, erasedLen|=1<<%%A"
  for %%B in (!originLen!) do if "!str1:~%%B,1!"=="" set /a "originLen&=~1<<%%A"
  for %%B in (!erasedLen!) do if "!str2:~%%B,1!"=="" set /a "erasedLen&=~1<<%%A"
)
set /a num=originLen-erasedLen
endlocal &set "num=%num%"

echo Number of percent signs: %num%
pause

Regards
aGerman

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: How to search and replace a string

#6 Post by timbertuck » 29 Dec 2011 12:06

thanks aGerman, a very elegant solution. I will incorporate this into my batch file. appreciate the help.

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: How to search and replace a string

#7 Post by timbertuck » 29 Dec 2011 15:41

timbertuck wrote:thanks aGerman, a very elegant solution. I will incorporate this into my batch file. appreciate the help.


now here is what i have
i need to substitute each % with ~ for as many times as it appears in the envar1.

Code: Select all

@echo off
setlocal enabledelayedexpansion
set envnum=envar%1
set envval=!envar%1!

echo %envval% > envfil.tmp

setlocal EnableDelayedExpansion
set "str1=A!envar%1!"
set "str2=A!envar%1:%%=!"
set /a "originLen=0, erasedLen=0"
for /l %%A in (12,-1,0) do (
  set /a "originLen|=1<<%%A, erasedLen|=1<<%%A"
  for %%B in (!originLen!) do if "!str1:~%%B,1!"=="" set /a "originLen&=~1<<%%A"
  for %%B in (!erasedLen!) do if "!str2:~%%B,1!"=="" set /a "erasedLen&=~1<<%%A"
)
set /a num=originLen-erasedLen
endlocal &set "num=%num%"
echo.
echo Number of percent signs to convert: %num%
goto run%num%

:doloop

:run0
echo nothing done
echo.
goto end

:run1
echo doing one substitution
for /f "tokens=1,2* delims=%%" %%a in ("!envval!") do set %envnum%=%%a~%%b
echo.
goto end

:end
echo set envar%1=!envar%1! >  envarpar.cmd
endlocal
call envarpar.cmd


if i do two subtitution's then :run2 is like this:

Code: Select all

:run2
echo doing two substitutions
for /f "tokens=1,2,3* delims=%%" %%a in ("!envval!") do set %envnum%=%%a~%%b~%%c
echo.
goto end

:end
echo set envar%1=!envar%1! >  envarpar.cmd
endlocal
call envarpar.cmd


not very neat as i will need a label to goto for each iteration of the bad string (at least 1-9 but could be more). im trying to figure out if i can make a more dynamic loop, but don't know how to make a one big statement that will incorporate substituting dynamically. maybe use the shift command (but thats for replaceable param's and don't know if maybe could program that functionality into the loop).

edit: might change this:

Code: Select all

for /f "tokens=1,2* delims=%%" %%a in ("!envval!") do set %envnum%=%%a~%%b~%%c
to this at least
for /f "tokens=1-%num%* delims=%%" %%a in ("!envval!") do set %envnum%=%%a~%%b~%%c

but how do i program in the add'l %b %c %d, etc?

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to search and replace a string

#8 Post by aGerman » 29 Dec 2011 16:09

timbertuck wrote:i need to substitute each % with ~ for as many times as it appears in the envar1.


I don't understand why you need the number of percent signs then. What about my suggestion in my first reply? That very short snippet substitudes all percent signs in the variable.

Regards
aGerman

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: How to search and replace a string

#9 Post by timbertuck » 29 Dec 2011 16:21

aGerman wrote:I don't understand why you need the number of percent signs then. What about my suggestion in my first reply? That very short snippet substitudes all percent signs in the variable.
Regards
aGerman


sorry must have missed something along the way then. let me backtrack and hunt down the culprit. i will check out your code and see what i have to do to make my code work. ( but yes for just one envar1, it works just fine)

thought: envar1 can be envar2-envar9 so that is why im looping and need the replaceable %a %b %c, etc. dont know if i mentioned that already.

was just looking at the macro thread, might be a germ of an idea with that.
sigh, back to the drawing board.

edit: upon further review, i see what your talking about. so i just loop your routine 9 times, and sub envar1 for 2-9. sometimes i need a reboot, lol. thanks for your help on this. will test this shortly.

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: How to search and replace a string

#10 Post by timbertuck » 30 Dec 2011 11:43

hmm stuck on something just general here..

if i have a string like so: set tmp1=string1" "string2

how can i parse that with delims? the following does not work:
for /f "tokens=1,2* delims= "" %a in ("%tmp1%") do echo a is %a &echo b is %b

what can i do to escape the first quotes in: delims= ""
so that %a is string1 and b is string2, both without a space or quotes.

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to search and replace a string

#11 Post by aGerman » 30 Dec 2011 12:13

Well, don't enclose the options in quotes but escape all possible separators (spaces, commas, equal signs, the quotation mark etc.) instead.

Code: Select all

for /f tokens^=1^,2*^ delims^=^"^  %a in ("%tmp1%") do echo a is %a &echo b is %b

Note: there are 2 spaces between the last caret and the %a. The first is escaped and belongs to the delimiters (quote and space in this case). The second separates the options from the variable.

Regards
aGerman

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How to search and replace a string

#12 Post by Squashman » 30 Dec 2011 12:17

aGerman wrote:Well, don't enclose the options in quotes but escape all possible separators (spaces, commas, equal signs, the quotation mark etc.) instead.

Code: Select all

for /f tokens^=1^,2*^ delims^=^"^  %a in ("%tmp1%") do echo a is %a &echo b is %b

Note: there are 2 spaces between the last caret and the %a. The first is escaped and belongs to the delimiters (quote and space in this case). The second separates the options from the variable.

Regards
aGerman

I was just researching this answer. Found a German website that explained it but could not even understand it with the Googles Translation. Glad you put it in plain english for me.
http://www.administrator.de/index.php?content=172844

aGerman
Expert
Posts: 4722
Joined: 22 Jan 2010 18:01
Location: Germany

Re: How to search and replace a string

#13 Post by aGerman » 30 Dec 2011 13:12

Yeah, that's where I found it first. Also jeb already shared the reference to this page.
It's a long story with small gist:
- If you need to use a quotation mark for an option in a FOR loop then remove the enclosing quotes.
but
- Escape the default separators: <space> and <tab>
- Escape special characters found in CMD /? help message: ()[]{}^=;!'+,`~
- Escape special characters used for redirections, pipes, concatenation: <>|&
- Escape the quotation mark: "
- If delayed expansion is enabled, escape exclamation marks: !

Regards
aGerman

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to search and replace a string

#14 Post by dbenham » 30 Dec 2011 13:37

Jeb posted a good summary of obscure FOR tricks: viewtopic.php?f=3&t=2385

Dave Benham

Post Reply