netcmh
asked on
Script to automate text file modification
Hello,
I download a file like this everyday:
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.
I download a file like this everyday:
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Toyed with calculation to make the subnet mask last night rather than lookup table but frankly prefer way Bill has done it!
ASKER
@billprew: Brilliant. Thank you. Works after I modified it a bit.
for /f "usebackq tokens=1-2 delims=/" %%A in (%InFile%) do (
for /f "usebackq tokens=1-2 delims=/" %%A in (%InFile%) do (
ASKER
Excellent!
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
~bp
ASKER
Thank you :)
Could you please define XXXX and YYYY?