Have a find/replace perl script thats broke

Hello Folks,

#!/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\.rich\.us/$ip{$1}/g;
           print;
     }
  }
},@ARGV;

When I run this it does it job, but I dont need the . ((dot)) after the IP

$ ./trythis.pl rich-input.txt *

option domain-name-servers 199.131.101.210., 199.131.101.161.;

I am trying to get it to look like this:

option domain-name-servers 179.131.101.210,129.131.101.161;

Thanks for your help in trying to TS this. I cant seem to figure this one out

What does the input look like?

the input file look like the below. Simple txt file

Foo.fs.rich.us 192.168.123.1
bar.fs.rich.us 200.100.32.4

Thanks

I don't see how your perl script could possibly produce that output from that input. The words 'option domain-name-servers', or even just 'option' if the 'domain-name-servers' is supposed to be part of the input string, are contained nowhere in it.

Post matching input and matching output. If they have nothing to do with each other they don't illustrate what your program is supposed to do...

Hi,

I have several files in a directory that I need to replace

Its working, but I end up with a . as I explained.

so lets say I am in /home/richsark/me/you/Data/dhcp

In there I have several sub directories like

/home/richsark/me/you/Data/dhcp/r1
/home/richsark/me/you/Data/dhcp/r4 etc...

In each of these directories, I have several files that contain lots of crap. I am only interested in changing these statements from within each file

from
option domain-name-servers Foo.fs.rich.us,bar.fs.rich.us;

to:

option domain-name-servers 192.168.123.1,200.100.32.4;

But my script is doing this:

option domain-name-servers 192.168.123.1., 200.100.32.4.;

I would like it to look like this:

option domain-name-servers 192.168.123.1,200.100.32.4;

I have this shell script, but this one lacks the comma and is a lot slower :slight_smile:

find /home/richsark/customers/me/Data/you/dhcp -type f ! -name "*.bak" |while read line
do
  awk 'NR==FNR{a[$1]=$2;next} !/^#/ {for (i in a) if ($0~i) sub(i ".fs.rich.us.",a)}1' rich-input.txt "$line" >temp 
#  echo mv temp "$line"
mv temp "$line"
done

This one looks like this

option domain-name-servers 192.168.123.1 200.100.32.4;

I would like it like so:

option domain-name-servers 192.168.123.1,200.100.32.4;

Try this.
Change the following line in your script:

...
           !/#/ && s/(\w+)\.fs\.rich\.us/$ip{$1}/g;
...

to this:

...
           !/#/ && s/(\w+\.fs\.rich\.us)/$ip{$1}/g;
...

Notice the position of the closing parenthesis.

tyler_durden

Cool.... Thank you.

Will test and revert

---------- Post updated 11-07-11 at 10:26 AM ---------- Previous update was 11-06-11 at 11:52 PM ----------

Hi,

I tested that change. Not looking good:

\#option domain-name-servers sv1.payette.r4.fs.rich.us, sv5.r4.fs.rich.us;
option domain-name-servers , ;

It changed the # one which I did not want, Plus with the wrong information. Also looks like it inserted the directory name in there. I know cause of the word r4 and r5

The the below is what I expected it to change.

option domain-name-servers 100.100.100.1,100.100.1.2;

Thanks, look fwd in your reply

Well, you never mentioned comments.

THIS IS WHY WE NEED MATCHING INPUT AND OUTPUT DATA. We can't see what you want without it.

So I guess that was a shot in the dark. Corona688 is right; we do really need matching input and output data.

Or maybe you could run the following three commands and paste the output over here:

cat rich-input.txt
 
cat /home/richsark/me/you/Data/dhcp/r1
 
cat /home/richsark/me/you/Data/dhcp/r4

If "/home/richsark/me/you/Data/dhcp" was just an example (as mentioned in your previous post), then replace it with the actual path of "r1". Ditto for "r4".

tyler_durden