[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2014-03-10
5
Medium Priority
?
2,257 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 1600 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 400 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Assume you have an outside contractor who comes in seasonally or once a week to do some work in your office, but you only want to give him access to the programs and files he needs and keep all other documents and programs private. Can you do this o…
While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

650 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