#!/usr/local/bin/perl # Copyright(C)1999,2001 by Daniel B. Sedory # # This PROGRAM creates a list of Formatted IP Addresses (one per line) # with evenly spaced groups of 3 digits each by inserting leading zeros # where necessary. For example: # # 1.23.4.56 will end up as 001.023.004.056 # # The OUTPUT FILE will be named: ipf.txt # ( INPUT files will NOT be changed ) # # One Reason for doing this: So you can SORT data rows in a database # by IP numbers with the numbers in the correct order; rather than # having 1.x.x.x, 11.x.x.x, 100.x.x.x all ending up together! # # Note: The entire file is read into memory, so don't use this # program on a 'monster-sized' DNS server file of some kind. print "\n This program makes NO changes to the INPUT file. The INPUT\n"; print " file must contain only a listing of IP Numbers (one per line!)\n"; print " It will ADD leading ZEROs to IPs such as 1.23.4.56 then\n"; print " save the Formatted IP numbers (001.023.004.056) to an OUTPUT\n"; print " file called: ipf.txt\n\n"; print " What is the file you wish to read? "; $inpfilename = ; chop($inpfilename); open (FILE, "$inpfilename") || die("Could not open the file $inpfilename"); @lines = ; close(FILE); open(PIPF, ">ipf.txt"); foreach $line (@lines) # Examine one line at a time. { @words = split(/\./, $line); # Split line into an array of 4 words # using the "dots" as separators. $words[3] =~ s/\n//; # Remove "newline char" from last. foreach $word (@words) { # Add leading zeros if necessary. $strlen = length($word); if ($strlen == 1) {$word = "00" . $word;} elsif ($strlen == 2) {$word = "0" . $word;} } # Put the line back together with "dots" : # Yes, the double-meaning was intended :-) # $line = $words[0]. "." . $words[1]. "." . $words[2]. "." . $words[3]; # # Some time after using the statement above, I changed the way to RE- # CREATE the line from its various parts... I just wanted you to see # the sad bit of humor above rather than erase it. Here's a better # way to do it using the JOIN function: $line = join (".", @words); print PIPF "$line\n"; # Write the new line and get another one } close(PIPF); exit; # Copyright(C)1999,2001 by Daniel B. Sedory