bat file to delete and replace text between two lines

August 21, 2019 at 14:33:21
Specs: Windows 10
Please can somebody help me I have a file ipaddress.config with a list of ip addresses and I need to update them on nearly 100 computers, wondering if there is a way to create a bat file to delete and replace all the ip addresses between and above these lines there are around 20 lines of text and below there is around 40 lines of text that I do not want to be changed as this is different on each computer

e.g

<sites default="127.0.0.1"> <---keep this
<site ipAddress="192.168.0.1" name="PC1" port="10001" number="1001"/> <--- replace
<site ipAddress="192.168.0.2" name="PC2" port="10001" number="1002"/> <--- replace
<site ipAddress="192.168.0.3" name="PC3" port="10001" number="1003"/> <--- replace
</sites> <--- keep this

there are over 200 ip addresses so doing this manually will take a lot of time hopefully somebody can give me a solution into this

the result should be

<sites default="127.0.0.1">
<site ipAddress="192.168.1.21" name="PC1" port="10001" number="1001"/>
<site ipAddress="192.168.1.22" name="PC2" port="10001" number="1002"/>
<site ipAddress="192.168.1.23" name="PC3" port="10001" number="1003"/>
</sites>

The ip addresses start on line 20. I can delete one line but not sure on how to delete all the lines between line 20 and line 136.


See More: bat file to delete and replace text between two lines

Reply ↓  Report •

#1
August 22, 2019 at 02:14:00
not quite sure if i fully understand, but just tell me what you are missing.
i mean i can generate all 255 lines if you want.

test.bat:

@echo off&setlocal enabledelayedexpansion
echo off>C:\Users\%username%\Desktop\output.txt
for /l %%a in (1,1,116) do (set/a var=20+%%a >nul&set/a var2=1000+%%a >nul&echo ^<site ipAddress="192.168.1.!var!" name="PC%%a" port="10001" number="!var2!"/^>)>>C:\Users\%username%\Desktop\output.txt

output:

<site ipAddress="192.168.1.21" name="PC1" port="10001" number="1001"/>
<site ipAddress="192.168.1.22" name="PC2" port="10001" number="1002"/>
<site ipAddress="192.168.1.23" name="PC3" port="10001" number="1003"/>
<site ipAddress="192.168.1.24" name="PC4" port="10001" number="1004"/>
<site ipAddress="192.168.1.25" name="PC5" port="10001" number="1005"/>
<site ipAddress="192.168.1.26" name="PC6" port="10001" number="1006"/>
<site ipAddress="192.168.1.27" name="PC7" port="10001" number="1007"/>
<site ipAddress="192.168.1.28" name="PC8" port="10001" number="1008"/>
<site ipAddress="192.168.1.29" name="PC9" port="10001" number="1009"/>
<site ipAddress="192.168.1.30" name="PC10" port="10001" number="1010"/>
<site ipAddress="192.168.1.31" name="PC11" port="10001" number="1011"/>
<site ipAddress="192.168.1.32" name="PC12" port="10001" number="1012"/>
<site ipAddress="192.168.1.33" name="PC13" port="10001" number="1013"/>
<site ipAddress="192.168.1.34" name="PC14" port="10001" number="1014"/>
<site ipAddress="192.168.1.35" name="PC15" port="10001" number="1015"/>
<site ipAddress="192.168.1.36" name="PC16" port="10001" number="1016"/>
<site ipAddress="192.168.1.37" name="PC17" port="10001" number="1017"/>
<site ipAddress="192.168.1.38" name="PC18" port="10001" number="1018"/>
<site ipAddress="192.168.1.39" name="PC19" port="10001" number="1019"/>
<site ipAddress="192.168.1.40" name="PC20" port="10001" number="1020"/>
<site ipAddress="192.168.1.41" name="PC21" port="10001" number="1021"/>
<site ipAddress="192.168.1.42" name="PC22" port="10001" number="1022"/>
<site ipAddress="192.168.1.43" name="PC23" port="10001" number="1023"/>
<site ipAddress="192.168.1.44" name="PC24" port="10001" number="1024"/>
<site ipAddress="192.168.1.45" name="PC25" port="10001" number="1025"/>
<site ipAddress="192.168.1.46" name="PC26" port="10001" number="1026"/>
<site ipAddress="192.168.1.47" name="PC27" port="10001" number="1027"/>
<site ipAddress="192.168.1.48" name="PC28" port="10001" number="1028"/>
<site ipAddress="192.168.1.49" name="PC29" port="10001" number="1029"/>
<site ipAddress="192.168.1.50" name="PC30" port="10001" number="1030"/>
<site ipAddress="192.168.1.51" name="PC31" port="10001" number="1031"/>
<site ipAddress="192.168.1.52" name="PC32" port="10001" number="1032"/>
<site ipAddress="192.168.1.53" name="PC33" port="10001" number="1033"/>
<site ipAddress="192.168.1.54" name="PC34" port="10001" number="1034"/>
<site ipAddress="192.168.1.55" name="PC35" port="10001" number="1035"/>
<site ipAddress="192.168.1.56" name="PC36" port="10001" number="1036"/>
<site ipAddress="192.168.1.57" name="PC37" port="10001" number="1037"/>
<site ipAddress="192.168.1.58" name="PC38" port="10001" number="1038"/>
<site ipAddress="192.168.1.59" name="PC39" port="10001" number="1039"/>
<site ipAddress="192.168.1.60" name="PC40" port="10001" number="1040"/>
<site ipAddress="192.168.1.61" name="PC41" port="10001" number="1041"/>
<site ipAddress="192.168.1.62" name="PC42" port="10001" number="1042"/>
<site ipAddress="192.168.1.63" name="PC43" port="10001" number="1043"/>
<site ipAddress="192.168.1.64" name="PC44" port="10001" number="1044"/>
<site ipAddress="192.168.1.65" name="PC45" port="10001" number="1045"/>
<site ipAddress="192.168.1.66" name="PC46" port="10001" number="1046"/>
<site ipAddress="192.168.1.67" name="PC47" port="10001" number="1047"/>
<site ipAddress="192.168.1.68" name="PC48" port="10001" number="1048"/>
<site ipAddress="192.168.1.69" name="PC49" port="10001" number="1049"/>
<site ipAddress="192.168.1.70" name="PC50" port="10001" number="1050"/>
<site ipAddress="192.168.1.71" name="PC51" port="10001" number="1051"/>
<site ipAddress="192.168.1.72" name="PC52" port="10001" number="1052"/>
<site ipAddress="192.168.1.73" name="PC53" port="10001" number="1053"/>
<site ipAddress="192.168.1.74" name="PC54" port="10001" number="1054"/>
<site ipAddress="192.168.1.75" name="PC55" port="10001" number="1055"/>
<site ipAddress="192.168.1.76" name="PC56" port="10001" number="1056"/>
<site ipAddress="192.168.1.77" name="PC57" port="10001" number="1057"/>
<site ipAddress="192.168.1.78" name="PC58" port="10001" number="1058"/>
<site ipAddress="192.168.1.79" name="PC59" port="10001" number="1059"/>
<site ipAddress="192.168.1.80" name="PC60" port="10001" number="1060"/>
<site ipAddress="192.168.1.81" name="PC61" port="10001" number="1061"/>
<site ipAddress="192.168.1.82" name="PC62" port="10001" number="1062"/>
<site ipAddress="192.168.1.83" name="PC63" port="10001" number="1063"/>
<site ipAddress="192.168.1.84" name="PC64" port="10001" number="1064"/>
<site ipAddress="192.168.1.85" name="PC65" port="10001" number="1065"/>
<site ipAddress="192.168.1.86" name="PC66" port="10001" number="1066"/>
<site ipAddress="192.168.1.87" name="PC67" port="10001" number="1067"/>
<site ipAddress="192.168.1.88" name="PC68" port="10001" number="1068"/>
<site ipAddress="192.168.1.89" name="PC69" port="10001" number="1069"/>
<site ipAddress="192.168.1.90" name="PC70" port="10001" number="1070"/>
<site ipAddress="192.168.1.91" name="PC71" port="10001" number="1071"/>
<site ipAddress="192.168.1.92" name="PC72" port="10001" number="1072"/>
<site ipAddress="192.168.1.93" name="PC73" port="10001" number="1073"/>
<site ipAddress="192.168.1.94" name="PC74" port="10001" number="1074"/>
<site ipAddress="192.168.1.95" name="PC75" port="10001" number="1075"/>
<site ipAddress="192.168.1.96" name="PC76" port="10001" number="1076"/>
<site ipAddress="192.168.1.97" name="PC77" port="10001" number="1077"/>
<site ipAddress="192.168.1.98" name="PC78" port="10001" number="1078"/>
<site ipAddress="192.168.1.99" name="PC79" port="10001" number="1079"/>
<site ipAddress="192.168.1.100" name="PC80" port="10001" number="1080"/>
<site ipAddress="192.168.1.101" name="PC81" port="10001" number="1081"/>
<site ipAddress="192.168.1.102" name="PC82" port="10001" number="1082"/>
<site ipAddress="192.168.1.103" name="PC83" port="10001" number="1083"/>
<site ipAddress="192.168.1.104" name="PC84" port="10001" number="1084"/>
<site ipAddress="192.168.1.105" name="PC85" port="10001" number="1085"/>
<site ipAddress="192.168.1.106" name="PC86" port="10001" number="1086"/>
<site ipAddress="192.168.1.107" name="PC87" port="10001" number="1087"/>
<site ipAddress="192.168.1.108" name="PC88" port="10001" number="1088"/>
<site ipAddress="192.168.1.109" name="PC89" port="10001" number="1089"/>
<site ipAddress="192.168.1.110" name="PC90" port="10001" number="1090"/>
<site ipAddress="192.168.1.111" name="PC91" port="10001" number="1091"/>
<site ipAddress="192.168.1.112" name="PC92" port="10001" number="1092"/>
<site ipAddress="192.168.1.113" name="PC93" port="10001" number="1093"/>
<site ipAddress="192.168.1.114" name="PC94" port="10001" number="1094"/>
<site ipAddress="192.168.1.115" name="PC95" port="10001" number="1095"/>
<site ipAddress="192.168.1.116" name="PC96" port="10001" number="1096"/>
<site ipAddress="192.168.1.117" name="PC97" port="10001" number="1097"/>
<site ipAddress="192.168.1.118" name="PC98" port="10001" number="1098"/>
<site ipAddress="192.168.1.119" name="PC99" port="10001" number="1099"/>
<site ipAddress="192.168.1.120" name="PC100" port="10001" number="1100"/>
<site ipAddress="192.168.1.121" name="PC101" port="10001" number="1101"/>
<site ipAddress="192.168.1.122" name="PC102" port="10001" number="1102"/>
<site ipAddress="192.168.1.123" name="PC103" port="10001" number="1103"/>
<site ipAddress="192.168.1.124" name="PC104" port="10001" number="1104"/>
<site ipAddress="192.168.1.125" name="PC105" port="10001" number="1105"/>
<site ipAddress="192.168.1.126" name="PC106" port="10001" number="1106"/>
<site ipAddress="192.168.1.127" name="PC107" port="10001" number="1107"/>
<site ipAddress="192.168.1.128" name="PC108" port="10001" number="1108"/>
<site ipAddress="192.168.1.129" name="PC109" port="10001" number="1109"/>
<site ipAddress="192.168.1.130" name="PC110" port="10001" number="1110"/>
<site ipAddress="192.168.1.131" name="PC111" port="10001" number="1111"/>
<site ipAddress="192.168.1.132" name="PC112" port="10001" number="1112"/>
<site ipAddress="192.168.1.133" name="PC113" port="10001" number="1113"/>
<site ipAddress="192.168.1.134" name="PC114" port="10001" number="1114"/>
<site ipAddress="192.168.1.135" name="PC115" port="10001" number="1115"/>
<site ipAddress="192.168.1.136" name="PC116" port="10001" number="1116"/>

i5-6600K[delid]@4.833GHz Core/4.632GHz cache@1.456v | 2x4GB Crucial-DDR4-2400MHzCL17@3018MHzCL12@1.45v | Sapphire Nitro+ SE RX 590 8GB@1650Mhz core@1.15v/2236MHz@1500MHz timings BiosModded


Reply ↓  Report •

#2
August 23, 2019 at 20:31:04
This attempts, feebly, to split the file (test) into two parts: temp1 (top) and temp2 (bottom).
Not sure how you handle the middle - instructions were somewhat lacking.
Warning: two temp files will be erased: temp1 and temp2.

::-------- begin prototype Frankenstein batch junk
@echo off & setlocal
for /f "tokens=1 delims=[]" %%a in ('find /n "sites default="^<test') do set s=%%a
for /f "tokens=1 delims=[]" %%a in ('find /n "</sites>"^<test') do set e=%%a
echo s: %s% e: %e%
pause
del temp1 2>nul
del temp2 2>nul
for /f "tokens=1* delims=[]" %%a in ('find /n /v ""^<test') do (
if "%%a" equ "%s%" goto :next
>>temp1 echo[%%b
)
:next
more +%e% test >> temp2
::---------- end of this jank
@hide: Kind of Overkill, don't ya think? I believe we got the idea about line three of output, Ha ha! ;) To save space, you can always use ...

message edited by nbrane


Reply ↓  Report •

#3
August 24, 2019 at 04:12:09
I have a txt document with all the ip address and pc numbers, they are all unique and don't need generating, I basically want to turn that txt file into a bat file which will do the following

With the document ipaddress.config
Save a copy then
After line Site default
Delete everything below it
But stop at line </sites>
Then add all the information below between the lines site default and </sites>

<site ipAddress="192.168.1.26" name="PC1" port="10001" number="1001"/>
<site ipAddress="192.168.1.45" name="PC2" port="10001" number="1002"/>
<site ipAddress="192.168.3.88" name="PC3" port="10001" number="1004"/>
<site ipAddress="192.168.1.23" name="PC9" port="10001" number="1009"/>
<site ipAddress="192.168.2.30" name="PC10" port="10001" number="1010"/>
<site ipAddress="192.168.3.1" name="PC11" port="10001" number="1011"/>
<site ipAddress="192.168.7.2" name="PC12" port="10001" number="1012"/>
<site ipAddress="192.168.1.31" name="PC13" port="10001" number="1013"/>
<site ipAddress="192.168.2.4" name="PC14" port="10001" number="1014"/>
<site ipAddress="192.168.1.5" name="PC15" port="10001" number="1015"/>

Save file


Reply ↓  Report •

Related Solutions

#4
August 24, 2019 at 18:07:30
Ok, I thought that might be the case. Here's a revised version:
@echo off & setlocal
del temp 2>nul
copy remote_file work
:: top portion
for /f "tokens=1 delims=[]" %%a in ('find /n /i "sites default="^<work') do (
for /f "tokens=1* delims=[]" %%b in ('find /n /v ""^<work') do (
if "%%b" equ "%%a" goto :next
>>temp echo[%%c
)
)
:next
:: append fig.txt - middle portion
copy temp+ipconfig.txt
:: append bottom portion
for /f "tokens=1 delims=[]" %%k in ('find /n /i "</sites>"^<work') do set /a p=%%k-1
more +%p% work>>temp

:: just the checking-up. remove for bulk processing once debugged
type temp
echo ctrl-c or remote_file will be modified:
pause
move temp remote_file


Reply ↓  Report •

#5
September 17, 2019 at 08:52:49
B E F O R E

Please can somebody help me I have a file ipaddress.config with a list of ip addresses and I need to update them on nearly 100 computers, wondering if there is a way to create a bat file to delete and replace all the ip addresses between and above these lines there are around 20 lines of text and below there is around 40 lines of text that I do not want to be changed as this is different on each computer
e.g
<sites default="127.0.0.1"> <---keep this

<site ipAddress="192.168.0.1" name="PC1" port="10001" number="1001"/> <--- replace
<site ipAddress="192.168.0.2" name="PC2" port="10001" number="1002"/> <--- replace
<site ipAddress="192.168.0.3" name="PC3" port="10001" number="1003"/> <--- replace
</sites> <--- keep this
there are over 200 ip addresses so doing this manually will take a lot of time hopefully somebody can give me a solution into this
the result should be
<sites default="127.0.0.1">
<site ipAddress="192.168.1.21" name="PC1" port="10001" number="1001"/>
<site ipAddress="192.168.1.22" name="PC2" port="10001" number="1002"/>
<site ipAddress="192.168.1.23" name="PC3" port="10001" number="1003"/>
</sites>
The ip addresses start on line 20. I can delete one line but not sure on how to delete all the lines between line 20 and line 136.


---------------------------------------------------------------------------------------------------------------------------
I have a 32 bit windows10 computer, and for the example given, I can use an old replace.com utility on your given data as:
Due to structural problems, I first replaced all quote marks with asteriks. I then replaced your 1, 2, 3 stuff, and then
replaced the replaced quote marks back. Quote marks have a special place in this utility. If you have asteriks in your files,
replace them with another ASCI character. I intentionally use a 32 bit windows10 computer just because I use this utility
a lot. It does not work on 64 bit windows computers computers(unless you use dosbox, or another workaround.) There are many
replace character utilities out there, but the few I looked at looked like a long learning curve, or longer than I care to look.

REPLACE Version 2.01.15 1987-88 (C) Gene Garapic (1:157/502)

I used this replace.com utility (it came out about the same time Microsoft made a same-named utility)
You have to be extremely careful with replace utilities and larg and numerous files. Back everything up before monkeying.

@echo off
replace.com ipaddress.config 34 42
replace.com ipaddress.config "8.0.1" "8.1.21">nul
replace.com ipaddress.config "8.0.2" "8.1.22">nul
replace.com ipaddress.config "8.0.3" "8.1.23">nul
replace.com ipaddress.config 42 34
----------------------------------------------------------------------------------------------------------------------------

A F T E R

Please can somebody help me I have a file ipaddress.config with a list of ip addresses and I need to update them on nearly 100 computers, wondering if there is a way to create a bat file to delete and replace all the ip addresses between and above these lines there are around 20 lines of text and below there is around 40 lines of text that I do not want to be changed as this is different on each computer
e.g
<sites default="127.0.0.1"> <---keep this
<site ipAddress="192.168.0.1" name="PC1" port="10001" number="1001"/> <--- replace
<site ipAddress="192.168.0.2" name="PC2" port="10001" number="1002"/> <--- replace
<site ipAddress="192.168.0.3" name="PC3" port="10001" number="1003"/> <--- replace
</sites> <--- keep this
there are over 200 ip addresses so doing this manually will take a lot of time hopefully somebody can give me a solution into this
the result should be
<sites default="127.0.0.1">
<site ipAddress="192.168.1.21" name="PC1" port="10001" number="1001"/>
<site ipAddress="192.168.1.22" name="PC2" port="10001" number="1002"/>
<site ipAddress="192.168.1.23" name="PC3" port="10001" number="1003"/>
</sites>
The ip addresses start on line 20. I can delete one line but not sure on how to delete all the lines between line 20 and line 136.


Reply ↓  Report •

#6
September 17, 2019 at 08:57:11
Whoops on mine - it worked, but I copied wrong file...Sorry

Reply ↓  Report •

#7
September 17, 2019 at 09:24:44
Now I look back, and it didn't work. Shame on me. I don't know why it didn't change all
quote signs to asterisks before doing the stuff it was supposed to do. I never did this
before trying to change quote marks first, and then the real work, due to internal
usage of quote marks in this utility. I've always used replace filename "this" "that" satisfactorally.
Crying in my beer on this one...

Reply ↓  Report •

#8
September 17, 2019 at 09:46:57
This utility didn't work because of the filename extension. (Extension too long - illegal filename)
I changed it to ipconfig.con, and it worked o.k. I didn't see any work done on-screen because
of the >nul ("screen quiet mode".

P.S. I still want my mommy after going through this debacle.


Reply ↓  Report •

Ask Question