• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 349
  • Last Modified:

find and replace in a directory using an input file

Hi folks,

I wrote a quick perl script that will find and replace one directory at at time with a bak up

see below:

I like to take this further to make it more efficient.

I have an input file with all the IP address to names formated like so in a txt file
addnsr1pri          166.7.3.101
addnsr1sec         166.2.100.78
addnsr2pri          166.2.220.140
addnsr2sec         166.3.68.105
addnsr3pri          166.3.68.103
addnsr3sec         166.5.4.249

I like to feed this input file and do a search and replace in a directory c:\dhcp
In that directory there are subfolders as well.

In addition, there are comment line with #, I dont need to touch those, leave as is please.

Can someone help with the creation of this modified script. I also dont need a .bak created.

Thanks
perl -e "s/option domain-name-servers addnsr4pri.fs.us, addnsr4sec.fs.us/option domain-name-servers 166.2.100.74,166.6.4.115/g;" -pi $(find . -type f)

Open in new window

0
richsark
Asked:
richsark
  • 8
  • 3
  • 2
1 Solution
 
oleggoldCommented:
0
 
oleggoldCommented:
Globally replace one string with another in a set of files

#!/bin/sh

#
# greplace
#
#
#

/usr/bin/perl -p -i -e "s/$1/$2/g" $3

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
richsarkAuthor Commented:
Hi Oleggold,

I am still a newbie, so forgive me.

So something like so?

So how do I tell it to use the input file in c:\dhcp and find and replace in c:\dhcp and all sub directories less the comment lines, without a back up?

Thanks for understanding
#!/usr/bin/perl -w
use strict;

# Open input file in read mode
open INPUTFILE, "rich-input.txt", $ARGV[0] or die $!;

# Read the input file line by line :
while (my $input_line = <INPUTFILE>) {

Open in new window

0
 
richsarkAuthor Commented:
Hi, just a jingle..<emg>
0
 
richsarkAuthor Commented:
HI,

I am still failing to compile this correctly...  
#!/usr/bin/perl -w
use strict;

# Open input file in read mode
open INPUTFILE, "rich-input.txt", $ARGV[0] or die $!;

# Read the input file line by line :
while (my $input_line = <INPUTFILE>) {
$modif = "s/option domain-name-servers / bar /g";
if (preg_match('/option domain-name-servers/i', ) {
preg_replace

Open in new window

0
 
richsarkAuthor Commented:
I added for the search to only look for a word, if a word is found replace with IP address (( I think I did it right ))


#!/usr/bin/perl -w
use strict;

# Open input file in read mode
open INPUTFILE, "rich-input.txt", $ARGV[0] or die $!;

# Read the input file line by line :
while (my $input_line = <INPUTFILE>) {
$modif = "s/option domain-name-servers / bar /g";
if (preg_match('/option domain-name-servers/i', ) {(?=.*[a-z])(?=.*[A-Z])
preg_replace

Open in new window

0
 
richsarkAuthor Commented:
Hi Folks, just seeing if there is anything you need more from me to help on this solution.

I apologize that I am a newbie on this sort of thing.

Any solution to the above would be must appreciated.

Thanks
0
 
ozoCommented:
#!/usr/bin/perl
use File::Find;
open F,"<input file with all the IP address to names" or die $!;
my %ip=map/(\S+)\s+(\S+)/,<F>;
close F;
find sub{
  if( -f ){
     local @ARGV=($_);
     local $^I="";
     while( <> ){
           !/#/ && s/(\w+)\.fs\.us/$ip{$1}/g;
           print;
     }
  }
},"c:/dhcp";
0
 
richsarkAuthor Commented:
Hello Ozo,

Thanks for you comment. A question on clarification.

On line 3:
Can I use what I indicated below to run like so:
open F,"<> $!;

PWD:
c:\dhcp\

./convert-dns.pl rich-input.txt *

folder c:\dhcp (( with sub-directories )) i.e c:\dhcp\r1 c:\dhcp\r2 c:\dhcp\w0 etc.....

it will work in all sub directories mention above?

and I would like to also confirm that your script will not touch any # lines that contain domain-name-server right?

 
#!/usr/bin/perl
use File::Find;
open F,"<> $!;
my %ip=map/(\S+)\s+(\S+)/,<F>;
close F;
find sub{
  if( -f ){
     local @ARGV=($_);
     local $^I="";
     while( <> ){
           !/#/ && s/(\w+)\.fs\.us/$ip{$1}/g;
           print;
     }
  }
},"c:/dhcp";

Open in new window

0
 
ozoCommented:
#!/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( <> ){
           !/#/ && s/(\w+)\.fs\.us/$ip{$1}/g;
           print;
     }
  }
},@ARGV;
0
 
richsarkAuthor Commented:
owesone ozo

So i can run it like so:

PWD:
c:\dhcp\

./convert-dns.pl rich-input.txt *

Just want to confirm.

Thank you so much Ozo
0
 
richsarkAuthor Commented:
Thanks Ozo !!

Your the best as always
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now