# Script to automate text file modification

Hello,

98.131.185.136
99.198.122.171
99.225.241.137
2.56.0.0/24
5.34.242.0/20
5.72.0.0/24

These are IP addresses. The ones purely numerical are individual devices. The ones with a /20 or /24 are networks, which can be translated as:

/30      255.255.255.252
/29      255.255.255.248
/28      255.255.255.240
/27      255.255.255.224
/26      255.255.255.192
/25      255.255.255.128
/24      255.255.255.0
/23      255.255.254.0
/22      255.255.252.0
/21      255.255.248.0
/20      255.255.240.0
/19      255.255.224.0
/18      255.255.192.0
/17      255.255.128.0
/16      255.255.0.0

I would like a script that can traverse through this very large file and make the following substitutions:

When it encounters something like this:

5.72.0.0/24

It should translate that as

XXXX 5.72.0.0 255.255.255.0

and so on.

And, when it comes across something like this:

99.198.122.171

It should translate it into

YYYY 99.198.122.171

I would like to do this in windows. I understand sed and awk can be used to for this, but I'll leave that to the experts.

Thank you.
LVL 21
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Hi there,

Could you please define XXXX and YYYY?
0
Commented:
Here's a BAT script that does what you described.  Adjust the paths as needed.

@echo off
setlocal EnableDelayedExpansion

set InFile=C:\EE\EE28250855\in.txt
set OutFile=C:\EE\EE28250855\out.txt

set map[30]=255.255.255.252
set map[29]=255.255.255.248
set map[28]=255.255.255.240
set map[27]=255.255.255.224
set map[26]=255.255.255.192
set map[25]=255.255.255.128
set map[24]=255.255.255.0
set map[23]=255.255.254.0
set map[22]=255.255.252.0
set map[21]=255.255.248.0
set map[20]=255.255.240.0
set map[19]=255.255.224.0
set map[18]=255.255.192.0
set map[17]=255.255.128.0
set map[16]=255.255.0.0

(
for /f "usebackq tokens=1-2 delims=/" %%A in ("%InFile%") do (
if "%%B" == "" (
echo YYYY %%A
) else (
echo XXXX %%A !map[%%B]!
)
)
)>"%OutFile%"

~bp
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

IT ConsultancyCommented:
Toyed with calculation to make the subnet mask last night rather than lookup table but frankly prefer way Bill has done it!
0
Author Commented:
@billprew: Brilliant. Thank you. Works after I modified it a bit.

for /f "usebackq tokens=1-2 delims=/" %%A in (%InFile%) do (
0
Author Commented:
Excellent!
0
Commented:
Glad that helped, thanks for the feedback.  It should work with the FOR the way I had it, looked like all you did was remove the quotes around the file name, but those are okay since we have the USEBACKQ option.  As long as you don't also have quotes in the SET above for INFILE.

~bp
0
Author Commented:
Thank you :)
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.