Have a find/replace perl script thats broke


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Have a find/replace perl script thats broke
# 1  
Old 11-06-2011
Have a find/replace perl script thats broke

Hello Folks,

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( <> ){
           !/#/ && 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
# 2  
Old 11-06-2011
What does the input look like?
# 3  
Old 11-06-2011
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
# 4  
Old 11-06-2011
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...
# 5  
Old 11-06-2011
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 Smilie

Code:
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[i])}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;

Last edited by richsark; 11-06-2011 at 11:00 PM.. Reason: add another script
# 6  
Old 11-06-2011
Try this.
Change the following line in your script:

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

to this:

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

Notice the position of the closing parenthesis.

tyler_durden
# 7  
Old 11-07-2011
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find and Replace String in Perl - Regexp

Trying to find and replace one string with another string in a file #!/usr/bin/perl $csd_table_path = "/file.ntab"; $find_str = '--bundle_type=021'; $repl_str = '--bundle_type=021 --target=/dev/disk1s2'; if( system("/usr/bin/perl -p -i -e 's/$find_str/$repl_str/' $csd_table_path")... (2 Replies)
Discussion started by: cillmor
2 Replies

2. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

3. Shell Programming and Scripting

Find and replace, perl code

Hi, Can somebody tell whats wrong with "find and replace perl code". I wanted to find "\n".write(status,data" and replace with "\n",data" in multipls files. #!/usr/bin/perl my @files = <*.c>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr0 =... (4 Replies)
Discussion started by: chettyravi
4 Replies

4. UNIX for Dummies Questions & Answers

Perl find & replace - what am I doing wrong?

Hi! I have a directory full of .plist type files from which I need to delete a line. Not every file contains the line, but of course I'd like to do it recursively. The line which I want to delete is: <string>com.apple.PhotoBooth</string> and looks like this in its native habitat: ... (9 Replies)
Discussion started by: sudon't
9 Replies

5. Shell Programming and Scripting

Character Find and replace in Unix or Perl

Hi, I am stuck with an problem and want some help, what i want to do is There is a directory name temp which include file named t1.txt, t2,txt, t3.txt and so on. These files contains data, but there are some bad character also that is % present in the files , I want to write the script... (13 Replies)
Discussion started by: parthmittal2007
13 Replies

6. Shell Programming and Scripting

perl find and replace

Hi, Please could someone advise, how I can resolve this issue with my find and replace command : perl -i -npe "s#RLM_LICENSE.*#RLM_LICENSE=$TEAM_TOP/licenses/abc.demo.lic#;" environment.properties $TEAM_TOP is an environment varible within my system. when i run this perl command from... (1 Reply)
Discussion started by: fettie
1 Replies

7. Shell Programming and Scripting

How to find the count and replace the particular part of string in perl?

Hi, I am taking the current time using localtime function in perl. For example if the time is: #Using localtime $time = "12:3:10"; I have to replace the value 3 (03) i.e second position to be 03. The output should be: 12:03:10 But if the other string for example: $str:... (1 Reply)
Discussion started by: vanitham
1 Replies

8. Shell Programming and Scripting

Broke Perl Script Second pair of eyes NET::FTPSSL

Hi all, Let me first start out by saying I'm a perl newbie and hope somebody can help, for the life of me I can't figure out why my script will not find and download a remote file via FTPSSL. What it's supposed to do is find the latest file named... (4 Replies)
Discussion started by: Styles
4 Replies

9. Shell Programming and Scripting

Find/replace to new file: ksh -> perl

I have korn shell script that genretaets 100 file based on template replacing the number. The template file is as below: $ cat template file number: NUMBER The shell script is as below: $ cat gen.sh #!/bin/ksh i=1; while ((i <= 100)); do sed "s/NUMBER/$i/" template > file_${i} ((... (1 Reply)
Discussion started by: McLan
1 Replies

10. Shell Programming and Scripting

Parentheses in perl find/replace

I'm trying to use the following command to do a batch find and replace in all commonly named files through a file hierarchy find . -name 'file' |xargs perl -pi -e 's/find/replace/g' which works fine except for a substitution involving parenthesis. As a specific example I'm trying to sub... (3 Replies)
Discussion started by: Jeffish
3 Replies
Login or Register to Ask a Question