Page 1 of 1

How to search and replace a string

Posted: 21 Dec 2011 14:35
by timbertuck
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?

Re: How to search and replace a string

Posted: 21 Dec 2011 15:01
by aGerman
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

Re: How to search and replace a string

Posted: 22 Dec 2011 13:14
by timbertuck
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

Re: How to search and replace a string

Posted: 29 Dec 2011 11:26
by timbertuck
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)?

Re: How to search and replace a string

Posted: 29 Dec 2011 11:57
by aGerman
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

Re: How to search and replace a string

Posted: 29 Dec 2011 12:06
by timbertuck
thanks aGerman, a very elegant solution. I will incorporate this into my batch file. appreciate the help.

Re: How to search and replace a string

Posted: 29 Dec 2011 15:41
by timbertuck
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?

Re: How to search and replace a string

Posted: 29 Dec 2011 16:09
by aGerman
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

Re: How to search and replace a string

Posted: 29 Dec 2011 16:21
by timbertuck
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.

Re: How to search and replace a string

Posted: 30 Dec 2011 11:43
by timbertuck
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.

Re: How to search and replace a string

Posted: 30 Dec 2011 12:13
by aGerman
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

Re: How to search and replace a string

Posted: 30 Dec 2011 12:17
by Squashman
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

Re: How to search and replace a string

Posted: 30 Dec 2011 13:12
by aGerman
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

Re: How to search and replace a string

Posted: 30 Dec 2011 13:37
by dbenham
Jeb posted a good summary of obscure FOR tricks: viewtopic.php?f=3&t=2385

Dave Benham