Perl Script not working on all directories

Hi Folks,

I have a script that I am using. The files are in Directory c:\files\change\options

In that directory I have many other sub folders like R1 R2 R5 E4 etc...

When I run this script in windows, It looks like its just changing the first folder R1 and not the rest.

Can I get an expert please to see why is not working.

I run it like

perl 2012-Convert-ISC-DHCP-FQDN--to--IP.pl rich-input.txt *

in Cygwin I also tried and same thing. Its just working on one folder

./2012-Convert-ISC-DHCP-FQDN--to--IP-Copy.pl rich-input.txt *

Here is the script

#!/usr/bin/perl
use File::Find;
open F,shift or die $!;
my %ip=map/(\S+)\s+(\S+)/,<F>;
close F;
find sub{
  if( -f ){
     local @ARGV=($_);
     local $^I="";
     while( <> ){
/^option\s+domain-name-servers\s.*/ &&  s/([\w.]*\w)\.?/$ip{$1}||($donthave{$1}=$1)/eg;
           print;
     }
  }
},@ARGV;
print "don't have $_ in my input file\n" for values %donthave;
  1. What is the exact requirement?
  2. What do you want to do in "other sub folders like R1 R2 R5 E4 etc"?

I have an input file, I need it to run in present directory and sub directories

The input file have a list of servers with ip addresses

Hello.test.com. 100.1.100.2
Foo.test.com. 20.20.12.1

Then it supposed to look for Any files or txt files that contain the bellow in its present directory and sub folders.

C:\test\sub\r1
C:\test\sub\rd
C:\test\sub\r6
C:\test\sub\e3
Etc.... I have almost 40 of them.

In file foo.txt or dhcp.conf (or any filename) look for this statement

option domain-name server hello.test.com,foo.test.com;

And change to
option domain-name server 100.1.100.2, 20.20.12.1;

I think I need this on line 15

},<@ARGV>;

Make sence?

---------- Post updated at 09:22 AM ---------- Previous update was at 12:20 AM ----------

Hi folks ... Any comment on above.

Thanks

---------- Post updated at 11:35 PM ---------- Previous update was at 09:22 AM ----------

As a follow up, I think I may have found out the cause.

The ones that are not changing is showing a tab or spaces on that line. If I backspace to the beginning of the line and rerun it it works.

No my silly question is where in my code above can I compensate for that?

Is line 11 the culprit? If so, would I need to remove the /^ part before option?

Thanks in advance for your help

In which file? - in "rich-input.txt" or in "foo.txt"/"dhcp.conf" etc?

tyler_durden

Hi Tyler ,

In response to your question... The contents in the directories. So dhcp.conf and anything else.

Input file is fine. No changes. Just the files/contents in the directories.

Thank you kindly.

In that case, change the following line in your program:

/^option\s+domain-name-servers\s.*/ &&  s/([\w.]*\w)\.?/$ip{$1}||($donthave{$1}=$1)/eg;

to this -

/^\s*option\s+domain-name-servers\s.*/ &&  s/([\w.]*\w)\.?/$ip{$1}||($donthave{$1}=$1)/eg;

It's like saying - if a line in any of my files has 0 or more whitespaces at the beginning, followed by the text I'm looking for ("option..."), then replace the domain name to IP address. Otherwise, push that information in a hash called "donthave", to be displayed later.

tyler_durden

1 Like

Fantastic..Tyler.

May I confirm my original code line blew up when it saw that space . Hence that was why it did not change.

Thank you agian.. Trying code in a few