Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace phone number on website 1

Status
Not open for further replies.

webdev17

Programmer
Feb 6, 2006
33
US
Could anyone tell me how I would write a perl script to look for a specific phone number within a website and replace it with a new phone number? Thanks!
 
? do you mean you want to scan all html files in a directory and do a text replace ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
what PERL level of experience are you?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Very minimal. So far my extent has just been to look at existing examples and try to modify the code to make it do what I need. But in this case, I don't have any examples to compare.
 
well the operation is fairly staight forward.

The perl creates an array of the directory (gets each file name)

you use the normal mechanism for opening the files in a loop and issue the substitue command.

then save the file and close the file handle.

however someone here might know a simple grep command to do this if you are on a *nix box, what's your environment?

I'm happy to help with the PERL code for my suggestion, but myself if this is the route you want to take.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Can we assume that you have access to put the script on the site, or in a higher directory on the actual server?

Is the telephone number always delimited the same way?

Are the pages dynamic? Is the info stored in a database? Do you have access to the database?

Can you provide an example of the html to ce changed if static?

Just a few questions
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Yes, I do have access to run the script on the site. We are running in a unix environment. The site is actually run through TeamSite and all of the pages we need to update are in templates to capture the data. The pages are then converted to html before they are published. So we would be updating the information in these data capture records, not the actual html (which I think I said above, but was incorrect).

The phone number will always be displayed and entered the same way (xxx-xxx-xxxx). The data is not stored in a database, it's stored in xml files.

Please let me know if there's anything else I can answer to clarify. Thanks so much!
 
so the phone numbers you want to replace are in XML files?

if they are all in one directory and you just want to replace X with Y i'm sure a simpe grep would do the job.

which unfortunately i'm no expert in. alternatively, something along these lines might work.

Code:
#!/usr/bin/perl

######################
# Set Error Trapping #
######################

use CGI::Carp qw(fatalsToBrowser warningsToBrowser); 
use warnings;
use strict;

my @files = glob('full_path_to_files_directory/*');

for(@files){
 open(CATAL, "+<$_");  
   flock(CATAL, 2);
   binmode(CATAL);
   my $file = <CATAL>;
   $file =~ s/oldnumber/newnumber/g;
   truncate(CATAL, length($file));
   seek(CATAL, 0, 0);
   print CATAL "$file";
   close(CATAL);
}

print "Replace Complete!";
exit();

something like that should do it, but this is off the top of my head so be sure to test anything before applying to the 'Real' xml files


"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
I'm a bit confused - you say the HTML is all generated dynamically from templates, using data from XML files. Surely this means the telephone number is only stored in one place, and just gets incorporated when the page gets built?

If it's not, then now might be a good time to fix it...
Code:
cd xmldir
grep -R 123-456-789 *
will show you all the places that the phone number appears in the XML. That should give you the tags that contain the phone numbers. Once you have these, you should be able to work out which templates are using which version of the phone number. Change them so they all point to the same place, and Bob's your paternal fraternal relation. The next time you have to change it, it will take you about five seconds.
 
steve, can grep be used to replace x with y ?



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
the grep steve posted is the unix(?) grep function, which returns a list that matches a search pattern, but I don't believe it can do substitutions. But you could use sed to do the substitutions.
 
Right, I knew there was a way to find/replace using a unix command. It's been so long since I worked in a *nix environment when I used to program in Informix and we used it all the time to do similar operations.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
you can do the same with perl too, something like

perl -pi.bak -e "s/Foo/Bar/g" <FileList>
 
Can you please explan what all of the items in this line of code mean?

perl -pi.bak -e "s/Foo/Bar/g" <FileList>

I'm assuming the information within the quotes is to find "Foo" which would be my current phone number and replace it with "Bar" which is the new phone number. And the "g" means you're doing it globally. But what is -pi.bak -e? And where it says <FileList> is that the path to where the files I want to search are stored?

Thanks!
 
I tried to run this command from unix to replace the phone number:

perl -pi.bak -e "s/123-456-7890/987-654-3210/g"

I'm getting the following error returned:
syntax error: 'newline or ;' unexpected

Any ideas? Thanks.
 
did you add the <FileList>, list of files?

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
This is probably a really dumb question, but do I literally type "<FileList>" or should that be pointing to a directory? Sorry I'm new to perl.
 
Just do it in Unix, use "grep" command to find which file its in. Then use the "sed" command to replace the phone number.

The phone number is probably located permanently in 1 file only. Just do this
Code:
     grep xxx-xxx-xxxx /dir/*.*
    
     sed -e 's/xxx-xxx-xxxx/123-456-7890/g' filename
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top