Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

format a telephone number
I have a lot of data in tab-delimited column form like this:

ABC Company   Houston, TX   34592   7604528734
DEF Widgets and Software   La Jolla, CA    92073    8583568236
and more ...

Company names and city names are of varying word length, which I can
deal with since it is tab delimited and ultimately I'll import this
into a mysql DB.

What I'd like to do before importing into mysql is format the telephone
number in the last column.  All of the telephone numbers are 10 digits
exactly, and I'd like to format them as (xxx) xxx-yyyy.

I've attempted this countless times and always wind up with a mess more
than anything else.  Have googled for "commify" and gotten good tips
but not a clean result yet.  Would sure appreciate any pointers.  I'm
using Gnu sed and nawk.

Thanks,
Kevin


Report this thread to moderator Post Follow-up to this message
Old Post
kosuke
04-10-05 08:55 AM


Re: format a telephone number
In article <1113098879.526511.214280@f14g2000cwb.googlegroups.com>,
"kosuke" <kevin@rustybear.com> wrote:

> I have a lot of data in tab-delimited column form like this:
>
> ABC Company   Houston, TX   34592   7604528734
> DEF Widgets and Software   La Jolla, CA    92073    8583568236
> and more ...
>
> Company names and city names are of varying word length, which I can
> deal with since it is tab delimited and ultimately I'll import this
> into a mysql DB.
>
> What I'd like to do before importing into mysql is format the telephone
> number in the last column.  All of the telephone numbers are 10 digits
> exactly, and I'd like to format them as (xxx) xxx-yyyy.
>
> I've attempted this countless times and always wind up with a mess more
> than anything else.  Have googled for "commify" and gotten good tips
> but not a clean result yet.  Would sure appreciate any pointers.  I'm
> using Gnu sed and nawk.
>
> Thanks,
> Kevin

awk '
BEGIN { FS="\t"; OFS="\t" }
{
areacode = substr($4,1,3)
exchange = substr($4,4,3)
number   = substr($4,7,4)
$4 = "(" areacode ") " exchange "-" number
print
}
'

Bob Harris

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
04-10-05 08:55 AM


Re: format a telephone number
Bob Harris <nospam.News.Bob@remove.Smith-Harris.us> writes:

> In article <1113098879.526511.214280@f14g2000cwb.googlegroups.com>,
>  "kosuke" <kevin@rustybear.com> wrote:
> 
[...] 

Littletested, off-topic here, and my sed is a bit rusty:
sed -e 's/\(...\)\(...\)\(....\)$/(\1) \2-\3/'

Ulrich
--
FX: A thunderclap, a loud wind, a plague of breakfast cereal, lava flows
under the raised-flooring, the earth opens, and D... suddenly goes Down,
Not Across.  A choir of little tentacled things sings, and he achieves
Recovery.                                       -- AdB

Report this thread to moderator Post Follow-up to this message
Old Post
Ulrich M. Schwarz
04-10-05 01:55 PM


Re: format a telephone number

Ulrich M. Schwarz wrote:
> Bob Harris <nospam.News.Bob@remove.Smith-Harris.us> writes:
>
> 
>
> [...]
> 
>
>
> Littletested, off-topic here, and my sed is a bit rusty:
> sed -e 's/\(...\)\(...\)\(....\)$/(\1) \2-\3/'

gawk has a very similair capability in gensub():

gawk '$NF=gensub(/(...)(...)(....)/,"(\\1) \\2-\\3","",$NF)'

if you feel like keeping it topical ;-)

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-10-05 08:55 PM


Re: format a telephone number
Terrific!  All 3 of these work with slight modifications.  Thanks
folks.
Kevin


Report this thread to moderator Post Follow-up to this message
Old Post
kosuke
04-11-05 01:55 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:12 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.