• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

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.
1 Solution
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

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!
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now