Solved

# find a string in each line in a file for Window batch  shell script

Posted on 2014-03-10
2,170 Views
Last Modified: 2014-03-10
Dear Expert,

I've one file called "junk.txt" in which I would like to replace Hostname and PortNumber
string with new value using window batch shell script , but I am spending more a few
hour in google, and still can not solve why I can not echo str variabie which
is always empty from following script code, Could you help on this, how to find
a string in string and replace with the new value. And the string addtion is "+" psosible ?

set newip="203.1.123.342"
set newport="00001779"
for /F  "tokens=*"  %%i in (junk.txt) do set str=%%i & echo %str% & if (%str:~1,9)=="HostName") str='"HostName"="'+'"+newip+'""

Please advise

Duncan

junk.txt file
=============
"Present"=dword:00000001
"HostName"="217.76.12.127"
"Protocol"="ssh"
"PortNumber"=dword:00001771
"CloseOnExit"=dword:00000001

0
Question by:duncanb7
• 3
5 Comments

LVL 83

Accepted Solution

oBdA earned 400 total points
ID: 39917997
Try it with this script; it will make a backup of the original file before changing the original:
@echo off
setlocal enabledelayedexpansion
set InputFile=C:\Temp\junk.txt
set NewHostName=203.1.123.342
set NewPort=00001779
set BackupFile=%InputFile%.bak
if not exist "%BackupFile%" copy "%InputFile%" "%BackupFile%" >NUL
if exist "%InputFile%" del "%InputFile%"
for /f "tokens=1-3 delims==:" %%a in ('type "%BackupFile%"') do (
if "%%~c"=="" (set Line=%%a=%%b) else (set Line=%%a=%%b:%%c)
if "%%~a"=="HostName" (set Line=%%a="%NewHostName%")
if "%%~a"=="PortNumber" (set Line=%%a=%%b:%NewPort%)
echo !Line!
>>"%InputFile%"	echo !Line!
)

0

LVL 43

Assisted Solution

Steve Knight earned 100 total points
ID: 39917998
Try this.  Few problems in there including within a single command like the for command you can't access variables set like your str entry unless you have delayedexpansion turned on (see line 2). Then you use !variable! to access.

Steve

@echo off
setlocal enabledelayedexpansion
set newip=203.1.123.342
set newport=00001779

cd /d c:\somedir\with\it\in

REM Delete previous go if needed and rename file to junk.old to process
del junk.old 2>NUL
rename junk.txt junk.old

REM Make backup copy of file
copy /y junk.old junk.bak

(for /F  "tokens=1,2 delims==" %%i in (junk.old) do (
set value=%%~j
if /i "%%~i"=="HostName" set value="+%newip%+"
if /i "%%~i"=="PortNumber" set value="%newport%"
echo %%i=%%j
)) > junk.txt

REM Remove this if wanted
type junk.txt
pause


Other alternatives are to use a util. to do it for you, e.g.

http://www.paulslore.com/utils/chgstr.zip
http://tools.tortoisesvn.net/grepWin
http://www.programmersheaven.com/download/41236/download.aspx
http://software.reinhardt.nu/ssr/index.htm
http://fart-it.sourceforge.net/
http://www.gnu.org/software/sed/
http://www.inforapid.de/html/searchreplace.htm
http://www.nodesoft.com/SearchAndReplace/Default.aspx
http://www.no-nonsense-software.com/freeware/
http://www.bestcode.com/html/findreplace.html
0

LVL 43

Expert Comment

ID: 39918003
I read there that you wanted "+" in the results, if not just remove the "+" characters.

Steve
0

LVL 13

Author Closing Comment

ID: 39918097
Thanks for all of your rely.

I tried obda 's code in one time, it works as I expect
and  dragon-it,  thanks for your code, I tried it that seems not working
even review it many times and check all  correct file typing and path.
The output junk.txt is same as before

Thanks for your attached link for the subject

Have a nice

Duncan
0

LVL 43

Expert Comment

ID: 39918245
Ok, no problem.  Worked for me here but then copy/pasted wrong version, line 19 should have been

echo %%i=!value!

Steve
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…

#### 773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.