On Tuesday 15 October 2002 10:10 pm, Bret is done writ:

> when I ran the code I sent using sub(oldip,newip) as you suggest,
> without the $ end of line placeholder, it changed addresses like
> 192.168.0.131 to 192.168.0.1231.  I will run run your code but assuming
> that it works, wy does it and mine did not?  Is there some magic in
> gsub?

Nope. I was jes' makin' sure...that any and all instances on a given line 
were changed.

Ok, found the problems, which was a *lot* easier once I had the data you were 
working with, and not some that I made up (I don't run DNS, since my SOHO is 
all of 4 machines, so I use assigned IPs in /etc/hosts). For example, I 
copied from a sample I googled, and it had a different format, with a ";" as 
the next field on the line after the serial number. This code will 
*guarantee* that this puppy will get it right, regardless of the format.

BEGIN {
    oldip="192.168.0.13";
    oldiplen = length(oldip);
    newip="192.168.0.123";
}
{
   if ($0 ~ /SOA/) {
     print $0;        # I like to be explicit - makes for easier reading     
getline;
     ser_no = $(1);
     restofline = substr( $0, index( $0, $1) + length($1));
      # which could also be done in several steps:
      #  start_of_ser_no = index( $0, $1 );
      #  end_of_ser_no = start_of_ser_no + length($1);
      #  restofline = substr( $0, end_of_ser_no );
     print "\t\t\t\t" ++ser_no restofline;
   }
   else {
      outline = $0;
     # checking the length in the if, below, guarantees that you won't
     #   get the 192.168.0.130, which is where the problem lay.
      if ( ($4 ~ oldip) && (length($4) == oldiplen )) 
         gsub( oldip, newip, outline );
      print outline;
   }
}
-- 
Message to Ashcroft:
"Necessity is the plea for every infringement of 
human freedom. It is the argument of tyrants; it is 
the creed of slaves." William Pitt, 1783 



-- 
redhat-list mailing list
unsubscribe mailto:[EMAIL PROTECTED]?subject=unsubscribe
https://listman.redhat.com/mailman/listinfo/redhat-list

Reply via email to