Computing.Net > Forums > Programming > Replace substrstring within a variable string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Replace substrstring within a variable string

Reply to Message Icon

Name: sammy1234
Date: September 21, 2009 at 12:36:55 Pacific
OS: Windows XP
Product: Microsoft Windows xp professional edition
Subcategory: Batch
Comment:

Hi All,

I am relatively new with batch scripting and I wonder if someone can help me out with a challenge.

I am trying to parse a text file and replace a parameter value within one of its lines. I have been able to store the line in a variable with FOR /F, but I can't seem to find the way to replace the specific string on it.

To make a long story short, let's assume that I have:

SET MYSTRING=args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m -Dsun.net.client.defaultConnectTimeout=1200000 -Dsun.net.client.defaultReadTimeout=1200000 -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 "-Djava.endorsed.dirs=C:\jboss-4.0.5\lib\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_02\lib\tools.jar" -classpath "C:\jboss-4.0.5\bin\run.jar" org.jboss.Main -c

My objective is to change the 1200000 value (which can be practically any value) in the string. Length substrings are not an option in this case, since the string might be of variable length and one or more parameters may be missing. I need to identify the specific -Dsun.net.client.defaultConnectTimeout= and -Dsun.net.client.defaultReadTimeout= parameters and then assign a custom value to them. Then the string should be replicated to the original format only with the parameter values changed. Something like:

MYSTRING="args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m -Dsun.net.client.defaultConnectTimeout=300000 -Dsun.net.client.defaultReadTimeout=300000 -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 "-Djava.endorsed.dirs=C:\jboss-4.0.5\lib\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_02\lib\tools.jar" -classpath "C:\jboss-4.0.5\bin\run.jar" org.jboss.Main -c"

I have been researching for some time to find a way, but no success.

I am aware that a batch file is far from being the best way to do this, but it's the only tool I can use for this purpose.

Any ideas will be very much appreciated.

Thanks!



Sponsored Link
Ads by Google

Response Number 1
Name: ghostdog
Date: September 21, 2009 at 17:51:15 Pacific
Reply:

vbscript

Set objFS=CreateObject("Scripting.FileSystemObject")
strFile = "c:\test\file"
strNewValue = "newvalue"
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
	strLine = Split(objFile.ReadLine," ")
	For i=0 To UBound(strLine)		
		If InStr(strLine(i),"client.defaultConnectTimeout")>0 Or _
			InStr(strLine(i) , "client.defaultReadTimeout")> 0 Then 
			s = Split(strLine(i),"=")
			s(UBound(s)) = strNewValue
			s = Join(s,"=")
			strLine(i) = s
		End If 
	Next
	strLine = Join(strLine," ")
	WScript.Echo strLine
Loop
objFile.Close

usage:

C:\test>more file
SET MYSTRING=args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m -Dsun.net.client.defaultConnect
Timeout=1200000 -Dsun.net.client.defaultReadTimeout=1200000 -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server
.gcInterval=3600000 "-Djava.endorsed.dirs=C:\jboss-4.0.5\lib\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_02\lib
\tools.jar" -classpath "C:\jboss-4.0.5\bin\run.jar" org.jboss.Main -c

C:\test>cscript /nologo test.vbs
SET MYSTRING=args=-server -Xrs -Xms640m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=128m -Dsun.net.client.defaultConnect
Timeout=newvalue -Dsun.net.client.defaultReadTimeout=newvalue -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.serv
er.gcInterval=3600000 "-Djava.endorsed.dirs=C:\jboss-4.0.5\lib\endorsed" -classpath "C:\Program Files\Java\jdk1.6.0_02\l
ib\tools.jar" -classpath "C:\jboss-4.0.5\bin\run.jar" org.jboss.Main -c

GNU win32 packages | Gawk


0

Response Number 2
Name: sammy1234
Date: September 21, 2009 at 23:36:57 Pacific
Reply:

Hi ghostdog,

Thanks much for the reply. I am not familiar with vbscript'ing at all, but I'll look into it. Do you have any link that can help to get me started on this?

Anyone else has an idea on how I can get this done with a batch script?

Thanks again!


0

Response Number 3
Name: ghostdog
Date: September 22, 2009 at 01:03:32 Pacific
Reply:

>>Do you have any link that can help to get me started on this?

see here

GNU win32 packages | Gawk


0

Response Number 4
Name: Mechanix2Go
Date: September 22, 2009 at 01:59:21 Pacific
Reply:

This starts with mystr.txt and produces newstr.txt. You may need to file off a few rough edges.

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

:: lesson learned:  echo !v%%i!
:: sammy.bat  Tue 22-09-2009 14:46:29.65

@echo off & setLocal enableDELAYedexpansion

set /p S=<mystr.txt
set N=

:loop
for /f "tokens=1* delims= " %%a in ("!S!") do (
  set /a N+=1
  set v!N!=%%a

    if "%%b" neq "" (
      set S=%%b
      goto :loop
    )
)

for /L %%i in (1 1 !N!) do (
  echo !v%%i! | find "defaultConnectTimeout" > nul
    if not errorlevel 1 (
      set v%%i=-Dsun.net.client.defaultConnectTimeout=3600000
    )
)

for /L %%i in (1 1 !N!) do (
  set new=!new! !v%%i!
)

> newstr.txt echo.!new!


=====================================
Helping others achieve escape felicity

M2


1

Response Number 5
Name: sammy1234
Date: September 24, 2009 at 00:49:33 Pacific
Reply:

Thanks a lot Mechanix. I haven't got the time to look into this, but as soon as I test it I will post the results.


1

Related Posts

See More



Response Number 6
Name: sammy1234
Date: September 25, 2009 at 00:28:49 Pacific
Reply:

Works beautifully.
Thank you very much.


0

Response Number 7
Name: sammy1234
Date: September 25, 2009 at 09:14:18 Pacific
Reply:

Now that I looked into it further I found a more concise and perhaps readable way based on Mechanix's idea, in case it helps anyone:

@echo off & setLocal enableDELAYedexpansion
set /p S=<mystr.txt
SET Result=

:loop
for /f "tokens=1* delims= " %%a in ("!S!") do (
  echo %%a | findstr "^-Dsun.net.client.defaultConnectTimeout">NUL  
  if not errorlevel 1 (
  SET Result=!Result! -Dsun.net.client.defaultConnectTimeout=555555
  ) ELSE (
  SET Result=!Result! %%a
  )
if "%%b" neq "" (
  set S=%%b
  goto :loop
  )
)

Echo %Result%


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Replace substrstring within a variable string

Find string inside a variable,batch www.computing.net/answers/programming/find-string-inside-a-variablebatch/12011.html

search and replace a string in ini www.computing.net/answers/programming/search-and-replace-a-string-in-ini-/16892.html

Piping text within a batch file www.computing.net/answers/programming/piping-text-within-a-batch-file/11068.html