although it probably won't break the regex in this situation .. you should
also include a \Q in front of $IPold to ensure that none of the variable is
used as a regex meta character
eg.
$line =~ s/\Q$IPold/$IPnew/;
what this does is the following .. presume that $IPold and $IPnew have been
initialised thusly
$IPold = '10.0.0.1';
$IPnew = '10.0.0.2';
then when you do the substitution the variable is interpolated first .. then
the regex is matched .. so the match actually does this
$line =~ s/10.0.0.1/10.0.0.2/;
now .. those '.'s in the regex part will match any character (they're
treated like a regex meta character) .. so the above will replace the
'10_0_0_1' in the following line
# some weird comment line containing 10_0_0_1
which will not be what you want .. the \Q tells Perl to quote any
metacharacter in the variable .. so the substitution would then become
$line =~ s/10\.0\.0\.1/10.0.0.2/;
which will do exactly what you want .. ie. it will replace 10.0.0.1 but not
10_0_0_1
--
jason king
In Spearfish, South Dakota, if three or more Indians are walking down
the street together, they can be considered a war party and fired
upon. - http://dumblaws.com/
>-----Original Message-----
>From: Collin Rogowski [mailto:[EMAIL PROTECTED]]
>Sent: Mon 23 Apr 2001 09:11
>To: Jon Tillman; [EMAIL PROTECTED]
>Subject: Re: introduction and first question
>
>
>you could use the s/// operator (substitute).
>
>try
>
>$line =~s/$IPold/$IPnew/;
>
>This will change the first occurence of $IPold in $line into $IPnew.
>If you want to change multiple occurences, use
>
>$line =~s/$IPold/$IPnew/g;
>
>g stands for global.
>
>hope this helps,
>
>cr
>
>On Sun, 22 Apr 2001 14:13:12 -0400, Jon Tillman said:
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Hi all,
>> I am a perl newbie, who is learning perl because I
>accidentally became a
>> sysadmin at work (sigh). My first task was a set of perl
>scripts to change
>> entries in .hosts files for BIND/DNS. The first one I wrote
>only needed to
>> change the TTL value in the file, and so I kinda munged it
>by using, in part:
>>
>> if ($line=~/\)$/) {
>> print FILEOUT " $ttl )\n";
>>
>> Now I need to write one that will update the IP address,
>wherever it is
>> given. Assuming that I have two variables $IPold and
>$IPnew, how would I
>> write a regex to replace the one above that will find all
>instances of $IPold
>> in a variable $line, and replace them with $IPnew?
>>
>> Here is some more code to put it in context:
>>
>> #!/usr/bin/perl
>>
>> use strict
>>
>> my $dir= shift || '.'; # work only in the dir it is called from
>>
>> print "--==++ Update MX records ++==--\n";
>> print "Enter old IP: ";
>> my $IP1=<STDIN>;
>> print "Enter new IP: ";
>> my $IP2=<STDIN>;
>> chomp $IP1; # remove newline chars
>> chomp $IP2; # remove newline chars
>>
>> opendir(DIR, $dir) or die "Cannot open $dir for reading: $!\n";
>> for my $file (grep(!/^\.\.?$/, readdir(DIR))) {
>> chomp $file;
>> next unless ($file =~ /\.hosts$/);
>>
>> open (FILEOUT, ">$dir/${file}.jonnew") or die "Cannot open
>> $dir/${file}.jonnew for writing: $!\n";
>> open (FILEIN, "$dir/$file") or die "Cannot open
>$dir/$file for reading:
>> $!\n";
>>
>> my $changed = 0;
>>
>> while (my $line = <FILEIN>) {
>> chomp ($line);
>> $line =~ s/\r$//;
>>
>> if ($line=~/mail IN A $IP1/) {
>> print FILEOUT "mail IN A $IP2\n";
>> $changed++;
>> } else {
>> print FILEOUT "$line\n";
>> }
>> }
>>
>> close (FILEIN);
>> close (FILEOUT);
>>
>> if ($changed) {
>> unlink("$dir/$file");
>> link("$dir/${file}.jonnew", "$dir/$file");
>> unlink("$dir/${file}.jonnew");
>> } else {
>> unlink("$dir/${file}.jonnew");
>> }
>>
>> }
>> closedir(DIR);
>>
>>
>>
>> - --
>> Jon Tillman
>> http://www.eruditum.org
>>
>> The worst thing about censorship is
>>
>> -----BEGIN PGP SIGNATURE-----
>> Version: PGP 6.5.1i
>>
>> iQA/AwUBOuMfOdga7tZtnIOtEQJmGACeKOS2ToMo5WM/ovxtaIRKBZ7/sikAn1/9
>> DMlkVxE1ITApG/6sco4yUBHn
>> =v21C
>> -----END PGP SIGNATURE-----
>>
>>
>