English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Im trying to write a Perl script that reads a text file containing telephone numbers in the form 00000-000000 (eg 05234-927951)
and then copies the numbers to a seperate text file. can anyone offer any advice, how should i detect the numbers?

2006-11-14 23:37:46 · 4 answers · asked by karljj1 4 in Computers & Internet Programming & Design

The file would contain various text as well, such as names, addresses etc. i just want to extract the numbers which will all be in that form, 5 numbers, - 6 numbers = 00000-000000

2006-11-14 23:53:08 · update #1

also if possible id like to write some details to the text file such as:

total numbers found,

the number of times each phone number was found, incase the same number is repeated

total number of unique telephone numbers found in the file and

2006-11-15 00:58:16 · update #2

4 answers

while (<>)
{
($n++, $uniq{$_}++, print("$_\n")) foreach /\+d-\d+)/g;
}
print STDERR "Total numbers: $n\n";
print STDERR "Unique numbers: scalar(%uniq)\n";
print STDERR "Individual number counts:\n";
print STDERR "$number: $coutn\n" while ($number, $count) = each %uniq;

It reads the original text from stdin, and prints the numbers to stdout, and the statitstics to stderr.
run it like this:
cat original_file.txt | perl thisprogram.pl > number_only.txt

The numbers will end up in numbers_only.txt, and the statistics will show up on your screen.
You can also give it the original file as argument instead of that cat thing if you prefer

2006-11-14 23:56:26 · answer #1 · answered by n0body 4 · 1 1

Can you describe the file a bit more...

Does it contain only phone numbers?
Are they comma separated? or there is a number on each line?

...stuff like that... that all affects how you approach a solution.

2006-11-14 23:45:31 · answer #2 · answered by Tamayi M 2 · 0 2

#!/usr/bin/perl -w
open(INPUT," open(OUTPUT,");
while(chomp()){
print OUTPUT "$1-$2\n" if /(\d{5})-(\d{6})/;
}
close INPUT;
close OUTPUT;
# end of program

/(\d{5})-(\d{6})/ actually matches for the 5digits followed by - and then 6 digits

2006-11-15 00:23:19 · answer #3 · answered by Y Raghavendra Reddy 2 · 2 0

my %phNumbers;
open (PHONE, "file.txt");
while (){
chomp;
if (/(\d{5}-\d{6})/){
$phNumbers{$1}++;
}
}
close(PHONE);
my $total;
open(OUT, ">outfile.txt");
foreach (keys %phNumbers){
print $_." found ".$phNumbers{$_}." times.\n";
$total += $phNumbers{$_};
}
print scalar(keys %phNumbers)." unique numbers.\n";
print "Total phone numbers found: ".$total."\n";
close(OUT);

2006-11-15 13:04:49 · answer #4 · answered by sterno73 3 · 1 0

fedest.com, questions and answers