[PLUG] Complex Search-and-Replace

John Purser jmpurser at gmail.com
Thu Nov 10 19:05:09 UTC 2005


On 11/10/05, Rich Shepard <rshepard at appl-ecosys.com> wrote:
>    I have a file of IP addresses, in dotted quad notation. Addresses can have
> 2, 3, or 4 of the quads, and I want to convert each address to CIDR. For
> example, if I have 123.456 I'd like to convert that to 123.256.0.0/16; if
> it's 123.456.789, then I'd like to convert that to 123.456.789.0/24.
>
>    I know there are multiple tools to do this; I suspect that sed would be the
> simplest. Am I better off searching for each group separately (e.g., all /16
> separate from all /24 and all /32)? Suggestions appreciated.
>
> Rich
>
> --
> Richard B. Shepard, Ph.D.               |   Author of "Quantifying Environmental
> Applied Ecosystem Services, Inc. (TM)   |  Impact Assessments Using Fuzzy Logic"
> <http://www.appl-ecosys.com>     Voice: 503-667-4517         Fax: 503-667-8863
> _______________________________________________
> PLUG mailing list
> PLUG at lists.pdxlinux.org
> http://lists.pdxlinux.org/mailman/listinfo/plug
>
Rich,

Just a couple of quick thoughts:
Remember that the CIDR notation isn't nailed to the dotted quad format
so just getting the quads won't do it.  It's either going to take a
heck of an if statement or you might want to convert the dotted quad
to a 32 bit number, then convert that to binary, then convert THAT to
a string.  Then count zeros from the right.  For that kind of
crunching I'd rather use python or even (if forced) perl.  Certainly a
shell script could do it.  I'd just rather have the flexibility of a
full scripting language.

I don't know how you got the data but remember that addess 123.456.789
might be from domain 123.456.  Therefore the CIDR would be 123.456/16,
not 123.456.789/24.



More information about the PLUG mailing list