Script to automate text file modification


I download a file like this everyday:

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:


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

When it encounters something like this:

It should translate that as


and so on.

And, when it comes across something like this:

It should translate it into


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.

Hi there,

Could you please define XXXX and YYYY?
Bill PrewCommented:
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]=
set map[29]=
set map[28]=
set map[27]=
set map[26]=
set map[25]=
set map[24]=
set map[23]=
set map[22]=
set map[21]=
set map[20]=
set map[19]=
set map[18]=
set map[17]=
set map[16]=

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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

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

Start your 7-day free trial
Steve KnightIT ConsultancyCommented:
Toyed with calculation to make the subnet mask last night rather than lookup table but frankly prefer way Bill has done it!
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

netcmhAuthor Commented:
@billprew: Brilliant. Thank you. Works after I modified it a bit.

for /f "usebackq tokens=1-2 delims=/" %%A in (%InFile%) do (
netcmhAuthor Commented:
Bill PrewCommented:
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.

netcmhAuthor Commented:
Thank you :)
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.