Perl Script not working on all directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script not working on all directories
# 1  
Old 02-27-2012
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


Code:
#!/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;

# 2  
Old 02-28-2012
Quote:
Originally Posted by richsark
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.
1. What is the exact requirement?
2. What do you want to do in "other sub folders like R1 R2 R5 E4 etc"?
# 3  
Old 02-29-2012
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

Last edited by richsark; 02-28-2012 at 10:19 AM..
# 4  
Old 02-29-2012
Quote:
Originally Posted by richsark
...
The ones that are not changing is showing a tab or spaces on that line.
...
In which file? - in "rich-input.txt" or in "foo.txt"/"dhcp.conf" etc?

tyler_durden
# 5  
Old 02-29-2012
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.
# 6  
Old 02-29-2012
Quote:
Originally Posted by richsark
...So dhcp.conf and anything else.
Input file is fine. No changes. Just the files/contents in the directories.
...
In that case, change the following line in your program:

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

to this -

Code:
/^\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
This User Gave Thanks to durden_tyler For This Post:
# 7  
Old 02-29-2012
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script stopped working

Hi, I have the following segment of a script which is supposed to prompt a user for password and then capture the password entered by the user. The function is called in by another script and used to work without issue, the problem is that recently the script is not waiting for the user to... (3 Replies)
Discussion started by: belalr
3 Replies

2. Shell Programming and Scripting

Perl: script to work with files with the same name in different directories

Hi All, I would like to use a Perl (not Bash) script to work with multiple files of the same name in different directories (all in the same parent directory). I tried to create a loop to do so, but it isn't working. My code so far: while (defined(my $file = glob("./*/filename.txt")) or... (1 Reply)
Discussion started by: elgo4
1 Replies

3. Shell Programming and Scripting

How to run perl script on multiple files of two directories?

Hi I have 100 files under file A labled 1.txt 2.txt.....100.txt(made up name) I have 1 files under file B labled name.txt How can i run the same perl script on 100 files and file name.txt I want to run perl script.pl A/1.txt B/name.txt perl script.pl A/2.txt B/name.txt ....... perl... (3 Replies)
Discussion started by: grace_shen
3 Replies

4. Shell Programming and Scripting

Perl script for finding directories with mtime

Need assistance in the perl script . Below script gives me the results of all the files and directories with mtime with no issues . But i wanted to have a file and specify all the directory locations and use that file as reference and get results . Any ideas are highly Appreciated . ... (6 Replies)
Discussion started by: ajayram_arya
6 Replies

5. Shell Programming and Scripting

executing perl script from another perl script : NOT WORKING

Hi Folks, I have 2 perl scripts and I need to execute 2nd perl script from the 1st perl script in WINDOWS. In the 1st perl script that I had, I am calling the 2nd script main.pl =========== print "This is my main script\n"; `perl C:\\Users\\sripathg\\Desktop\\scripts\\hi.pl`; ... (3 Replies)
Discussion started by: giridhar276
3 Replies

6. Shell Programming and Scripting

Run perl script on files in multiple directories

Hi, I want to run a Perl script on multiple files, with same name ("Data.txt") but in different directories (eg : 2010_06_09_A/Data.txt, 2010_06_09_B/Data.txt). I know how to run this perl script on files in the same directory like: for $i in *.txt do perl myscript.pl $i > $i.new... (8 Replies)
Discussion started by: ad23
8 Replies

7. Shell Programming and Scripting

Perl script 'system' linking to local shell script not working

Trying to figure out why this works: printpwd.pl #!/usr/bin/perl use CGI::Carp qw( fatalsToBrowser ); print "Content-type: text/html\n\n"; $A = system("pwd"); $A = `pwd`; print "$A\n"; ^^actually that works/breaks if that makes any sense.. i get the working directory twice but when... (5 Replies)
Discussion started by: phpfreak
5 Replies

8. UNIX for Dummies Questions & Answers

Perl script not working

Hi Experts!! I have written a very simple script in perl.The script is : $ cat 1.pl #!/usr/bin/perl print "Hi there!\n"; When i ran the above perl script it is showing the following error: $ perl 1.pl -ksh: cd: bad substitution Can anybody,help on this ....as why this script is... (1 Reply)
Discussion started by: Amey Joshi
1 Replies

9. Shell Programming and Scripting

how to change working directories in perl?

i am new to perl. i am writing a perl script. i want to know how to change the working directories? for ex. i have a perl script in c:\proj\ . i want to run this script in this directory but i need my script to change its working directory to D:\xyz\ dynamically in the script. your help is... (1 Reply)
Discussion started by: megastar
1 Replies

10. Shell Programming and Scripting

perl package directories - what if the script is diff DIR to the one contain *.pm?

Hi there, say the package is in the ~/ and it's ~/packageFoo.pm I can use usePackage.pl in ~/ (~/usePackage.pl). Now, if I move it to ~/subDIR/usePackage.pl, the script won't work because it's not in the same DIR with packageFoo.pm How can i fix it? Thanks Gusla (1 Reply)
Discussion started by: gusla
1 Replies
Login or Register to Ask a Question