Extract line from text file, modify and reinsert it

July 24, 2012 at 14:06:59
Specs: Windows 7, Varies
Given:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Think_Big\Shared\Files]
"ConfigurationMode"=dword:00000000
"Transport"=dword:00000004
"ServerSipUri"="user@host.somewhere.com"
"ServerUserName"="old_server\user_host_"
"SavePassword"=dword:00000001

I want to copy the contents of the ServerSipUri line (i.e. user@host.somewhere.com) into the ServerUserName line (i.e. replacing old_server\user_host_) while also removing the somewhere and one of the dots from the line.

Example output needed:

"ServerUserName"="user@host.com"

Been busting my brains out for a while on this one - something this simple HAS to be possible. If this was required one one or a handful of systems, I could do it by hand - but since we're talking a good number of systems, and some in remote locations, I'd like to do this in an automated fashion.

Thanks in advance,
--- A


See More: Extract line from text file, modify and reinsert it

Report •

#1
July 25, 2012 at 01:28:17
:: ===== script starts here ===============
::
:: acho.bat 2012-07-25 13:58:00.57
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "tokens=1-2 delims==" %%a in (myfile) do (
if %%a equ "ServerSipUri" (
set S=%%b
call :sub1
)
if %%a equ "ServerUserName" ( echo.%%a=!S!
) else (
echo.%%a=%%b
)
)>> newfile
goto :eof

:sub1
set S=!S:somewhere.=!
goto :eof
::====== script ends here =================


====================================
Life is too important to be taken seriously.

M2


Report •

#2
July 25, 2012 at 07:26:57
Thanks for the script, but I'm having some issues with it.

Running the script (as written but modified for the file that I'm using to test with), I get no output - newfile gets created but it zero length. If I change the @echo off construct to simply echo, I get an "Echo is ON" response.

I've also tried working with the for syntax - changing the tokens to * and delims to just " to make sure that everything gets read and still the same result.

And finally, I've tried adding some more echo statements to see what the values of the variables are during the process, and still no output.


Report •

#3
July 25, 2012 at 14:31:59
Try this:

@echo off & setlocal enabledelayedexpansion
set file=test.reg
set tld=com
if exist newfile.txt del newfile.txt
for /f "tokens=*" %%i in (%file%) do (
	set line=%%i
	if "!line:~1,12!"=="ServerSipUri" (
		call :_ParseServerSipUri
	)
	if "!line:~1,14!"=="ServerUserName" (
		set line="ServerUserName"="!ServerUserName!"
		>>newfile.txt echo !line!
	) else (
		>>newfile.txt echo !line!
	)
)
goto :eof
:_ParseServerSipUri
for /f "tokens=1 delims=." %%a in ('echo !line!') do (
	set _tmp_=%%a.%tld%
	for /f "tokens=2 delims==" %%z in ('echo !_tmp_!') do (
		set ServerUserName=%%~z
	)
)
goto :eof

Tony


Report •

Related Solutions

#4
July 25, 2012 at 20:51:43
And here's another one to try:
@echo off>tst & setlocal enabledelayedexpansion
set xout=tst
SET SH="ServerSipUri"
for /f "tokens=1-3* delims=.=" %%a in (reg) do (
set out=!xout!
set xout=tst
if **%%a** equ **!SH!** (
>> !out! echo %%a=%%b.%%c.%%d
>> !out! echo "ServerUserName"=%%b.%%d
set xout=nul
) else (
>>!out! echo %%a=%%b
)
)
:end
feeble, Oh, so feeble... :-(

Report •

#5
July 25, 2012 at 21:58:58
Or just copy your file to MYFILE and watch it work.


====================================
Life is too important to be taken seriously.

M2


Report •

#6
July 26, 2012 at 08:32:23
"Or just copy your file to MYFILE and watch it work."

What's wrong with people offering alternative solutions?

Tony


Report •

#7
July 26, 2012 at 10:43:12
if you are trying to see the echo, remove the whole line @echo off

:: mike


Report •

Ask Question