NAME EXT RM# ORG NAME EXT RM# ORG ------------------------------------- -------------------------------------- - A - BASILE, YYYY 5555 1H08 IAMG ABEND, YYYYYY 5555 2014 CE BATES, YYYY 5555 4832 BT ABRAMS, YYYYY 5555 C-07 BATHERSFIELD, YY 5555 B-39 CE ADAMS, YYYY 5555 255 OTC BAXTER, YYYY 5555 A-43 ADAMS, YYYY 5555 149 BT BEAR, YYYYYY 5555 H42 ATO ADAMS, YYYYYYY 5555 A-16 BEASLEY, YYY 5555 D-79 ADUAKA, YYYYYYYY 5555 A-52 BEATTY, YY 5555 4832 TAG AHMED, YYYYYY 5555 C-63 BECHTLE, YYYY 5555 D-26 AHMED, C. YYYYYY 5555 D-69 SOMEU BEDOYA, YYYYYYYY 5555 CEAs you can see, they use things like "- A -" to denote the start of an alphabetical section, which I've had a hard time with. Also, some folks have middle initials, some don't, some have an organization, some don't.
This is really a quick hack, so it's not as clean as I'd like.#!/usr/bin/env perl use strict; use Text::Soundex; my $PHONE_LIST = "$ENV{HOME}/phone_list"; my %seen; my $cntr; if(@ARGV == 0 || $ARGV[0] eq '-?' || $ARGV[0] eq '-h') { print "\nUsage: $0 <last name> ...\n\n"; exit 1; } open(PL, "$PHONE_LIST") || die "Can't open $PHONE_LIST: $!"; while(<PL>) { foreach my $name (@ARGV) { $name =~ /\U$name/; my $code = soundex($name); my $column_one = my $column_two = $_; my $flag; $column_one =~ s/^\s-*\s*(\w+)\s*-*,*.*$/$1/g; $column_two =~ s/^\s\w+,\s.* (\w+),.*/$1/g; if($column_one && soundex($column_one) eq $code) { $flag = 1; } elsif($column_two && soundex($column_two) eq $code) { $flag = 2; } if($flag) { $seen{$name}++; if(++$cntr == 1) { printf("\n%-12s %-15s %-8s %-5s\n", "First", "Last", "Ext", "Rm#"); print "=" x 43, "\n"; } if($flag == 1) { s/^\s(\w+), (\w+)\s+(\w?)\s?(\d{4,10})\s+(\S+)\s*.*$/$ +2 $1 $3 $4 $5/g; } elsif($flag == 2) { s/^\s.*\s(\w+), (\w+)\s+(\w?)\s?(\d{4,10})\s+(\S*)\s+. +*$/$2 $1 $3 $4 $5/g; } my ($fname, $lname, $m, $num, $room) = split(/ /); chomp($room); printf("%-10s %1s %-15s %-8d %-5s\n", $fname, $m, $lname, $num, $room); } } } foreach my $name (@ARGV) { $name =~ /\U$name/; if(! exists($seen{$name})) { print "\nNot found: $name"; } } print "\n"; close(PL);
In reply to Need help with regex by BastardOperator
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |