Gossamer Forum
Home : General : Perl Programming :

A script to clean up a phone directory please.

Quote Reply
A script to clean up a phone directory please.
Hello all.

I have a text file that is in the format like below. A general city phone book.
There are two basic formats. 1) which has the phone extension and 2) which doesn't.

1) Amador, Jodi - Clerk Typist, Fire 4-4976 44976
2) Amicangelo, Michael D. - Utilities Engineer, WWTP 971-4834

I would like to write a script that creates a text file with the persons full name and phone number only. No locations, titles or extensions.

I will then use the file to create a database

I would like to throw in a ; to use as a delimiter.

The results should look like this

Amador, Jodi; 4-4976
Amicangelo, Michael D.; 997-4834

I am not that experienced with regular expressions.
I am a beginner perl programmer.
Please point me in some direction. Thanks
Jeem
Quote Reply
Re: [jeempc] A script to clean up a phone directory please. In reply to
At a guess:

Code:
/^([^a-zA-Z,\.\s]+)\s-[^,]+,\s[a-zA-Z]+\s([\d-]+)/ and print FILEHANDLE "$1;$2\n";

It's likely to need tweaking.

Last edited by:

Paul: Feb 27, 2003, 11:18 AM
Quote Reply
Re: [Paul] A script to clean up a phone directory please. In reply to
I guess I'm more of a beginner than I thought. I get lost right after the regex. I'm not sure how to use this in a program. I will check my books. Thanks and I'll let you know if it works. Any other responses to Pauls reply would be very appreciated.
Thanks
Jim
Quote Reply
Re: [jeempc] A script to clean up a phone directory please. In reply to
You'd open the original file, loop it, then add my line which would print to the new file - something like this:

Code:
open OLD, "original file" or die $!;
open NEW, ">new file" or die $!;
while (<OLD>) {
chomp;
/^([^a-zA-Z,\.\s]+)\s-[^,]+,\s[a-zA-Z]+\s([\d-]+)/ and print NEW "$1;$2\n";
}
close NEW;
close OLD;

Last edited by:

Paul: Feb 27, 2003, 1:31 PM
Quote Reply
Re: [Paul] A script to clean up a phone directory please. In reply to
i think it should be

Code:
/^([a-zA-Z,\.\s]+).....


or, did I miss something?
Quote Reply
Re: [adrockjames] A script to clean up a phone directory please. In reply to
Yeah my fault. I was originally using [^-]+ and then decided to change it but forgot to remove the ^
Quote Reply
Re: [Paul] A script to clean up a phone directory please. In reply to
Thank you all very much.