Solved

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

Posted on 2014-03-10
5
2,223 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

Open in new window

0
Comment
Question by:duncanb7
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 85

Accepted Solution

by:
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!
)

Open in new window

0
 
LVL 43

Assisted Solution

by:Steve Knight
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

Open in new window


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

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

Steve
0
 
LVL 13

Author Closing Comment

by:duncanb7
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

by:Steve Knight
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

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

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

Join & Ask a Question