In our organization, there are lot of users who do not have their phonenumber in proper format. the correct format is : xxx-xxxx. Therefore, i've pulled out a all users report, consisting of two columns, one with name and other with phonenumber, the report is in csv format.

Does any one have any script or help me to create one, to segrate users with incorrect format phonenumbers. ?

Thanks, Prince

7 Years
Discussion Span
Last Post by d5e5

I don't have a script that does it but if you create and show us an example with a few correct and incorrect phone numbers I'm sure somebody could help you make a script that prints all the correct and / or the incorrect lines separately.

We need to know if your file has a header line at the start, whether the names are enclosed in quotes, etc. Not all csv files are formatted the same. Microsoft Excel creates them one way but other programs vary the format a bit. I understand you don't want to give us real names and numbers, but if you can make up a few it will give us something to test.


Thanks for reply.

The report looks something like this, it is in csv format :

Rita, 555-6754
John, 888 9876
Akash, 767-
Sue, 67

My intension is create a report, which will contain users like John,Akash and Sue. They are the users, without xxx-xxxx format.

Thanks in Advance.



I put your sample data into a file called PhoneNbrs.txt in the same directory where I wrote the following script:

use strict;
use warnings;
my $phones = "PhoneNbrs.txt";
my $phones_errors = "PhoneNbrsErrors.txt";
open (FIN, '<', $phones); #Open file for reading
my $header = <FIN>; #Read first record of file to get past it.
open (FOUT, '>', $phones_errors); #Open file for writing
while (<FIN>) { #Read each remaining record into $_ variable, one at a time
    if ($_ =~ m/,\s*\d\d\d-\d\d\d\d\s*$/) { #If the current record matches valid number pattern
        next # Go to the top of the loop to read the next record (skip this one)
    else {
        print FOUT; #Write this record into output file
print "Done. Look in $phones_errors for errors (if any).\n";
close FIN;
close FOUT;

Edited by d5e5: n/a


You're welcome. And thanks for marking the thread solved.:) A lot of people forget to do that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.