Solved

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

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

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You may have a outside contractor who comes in once a week or seasonal to do some work in your office but you only want to give him access to the programs and files he needs and keep privet all other documents and programs, can you do this on a loca…
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

726 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