Hi,

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

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 :

Name,PhoneNumber
Rita, 555-6754
Prince,887-6543
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.

Prince

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

#!/usr/bin/perl
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;

It worked. I really appreciate your efforts on this.

Thank You. Prince

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.