Solved

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

Posted on 2014-03-10
5
2,147 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
  • 3
5 Comments
 
LVL 83

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now