Skip to content

Instantly share code, notes, and snippets.

@rsvp
Created August 16, 2012 15:50
Show Gist options
  • Save rsvp/3371261 to your computer and use it in GitHub Desktop.
Save rsvp/3371261 to your computer and use it in GitHub Desktop.
telamerica.sh : look-up North American region / area code within +1 | Linux bash self-contained database
#!/usr/bin/env bash
# bash 4.1.5(1) Linux Ubuntu 10.04 Date : 2012-08-11
#
# _______________| telamerica : look-up North American region / area code.
#
# Usage: telamerica [term]
# ^area code (three digits work best),
# or case-sensitive partial regional name.
#
# Examples: % telamerica Mexico
# 505.575; New Mexico
# # i.e. Mexico is not considered North America.
# # Area codes are separated by period.
#
# % telamerica 575
# 575; New Mexico
#
# Dependencies: awk
# CHANGE LOG get LATEST version from https://bitbucket.org/rsvp/gists/src
#
# 2012-08-11 First version. Telephone data massaged from:
# http://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes
# California region is broken down into more specific areas.
# Area codes planned for future relief are included.
# Special three-digit codes are also included.
#
# See also:
# Telephone numbering plan,
# https://en.wikipedia.org/wiki/Area_codes for history and syntax.
# North American numbering plan,
# https://en.wikipedia.org/wiki/North_American_Numbering_Plan
# The +1 country code includes far more than just the USA!
#
# NPA:= Numbering Plan Area Code.
# NPA cannot begin with 0 or 1.
# NPA cannot have 9 as its middle digit.
# Each area code may contain up to 7,919,900 unique phone numbers.
# Codes ending in double digits are reserved as easily recognizable
# codes (ERCs), to be used for special purposes such as toll-free
# 800, 888, 877, 866 and 855, personal 700 numbers, and high-toll
# 900 numbers, rather than for geographic areas.
# Most North American mobile phones are assigned the same
# locality-specific codes as landlines.
# _____ Prelims
set -u
# ^ unbound (i.e. unassigned) variables shall be errors.
# Example of default assignment: arg1=${1:-'foo'}
set -e
# ^ error checking :: Highly Recommended (caveat: you can't check $? later).
#
# _______________ :: BEGIN Script ::::::::::::::::::::::::::::::::::::::::
term=${*:-'California$'}
outf='/dev/shm/88_areacode-out.tmp'
tmpf='/dev/shm/88_areacode.tmp'
# Write to TMP FILE data on 92 regions such that:
# California region is broken down further into specific cities/counties.
# Special three-digit codes are listed at the end.
# Unicode characters have been converted to ASCII equivalent.
# Area codes are separated by period.
# Region and area code are separated by @.
cat - > $tmpf <<EOHereDoc
Alabama@205.251.256.334.659.938
Alaska@907.250
Arizona@480.520.602.623.928
Arkansas@327.479.501.870
California@209.213.310.323.341.369.408.415.424.442.510.530.559.562.619.626.627.628.650.657.661.669.707.714.747.760.764.805.818.831.858.909.916.925.935.949.951
California (Stockton, Modesto, Merced, Tracy, San Andreas, Yosemite National Park)@209
California (Downtown Los Angeles)@213
California (Beverly Hills, Brentwood, Malibu, Santa Monica, Torrance, Santa Catalina Island)@310
California (Los Angeles excluding Downtown, Western Los Angeles, Westwood, San Fernando Valley, Florence)@323
California (Oakland, Fremont, Hayward, Richmond, Berkeley, Alameda)@341
California (Vallejo, Crescent City, Eureka, Redwoods National Park, Santa Rosa, Ukiah)@369
California (San Jose, Cupertino, Los Gatos, Milpitas, Mountain View, Santa Clara, Sunnyvale, Silicon Valley)@408
California (San Francisco, Marin County)@415
California (Beverly Hills, Brentwood, Malibu, Santa Monica, Torrance, Santa Catalina Island)@424
California (Bishop, Ridgecrest, San Bernardino County, Riverside County, Palm Springs, Palm Desert)@442
California (Oakland, Fremont, Hayward, Richmond, Berkeley, Alameda)@510
California (Redding, Auburn, Chico, Davis, Lake Tahoe, Placerville, Susanville, Truckee, Yreka)@530
California (Fresno, Hanford, Madera, Tulare, Visalia, Central Valley)@559
California (Long Beach, Whittier, Norwalk, La Habra, Lakewood, Pico Rivera)@562
California (San Diego, Chula Vista, National City, El Cajon, La Mesa, Lemon Grove)@619
California (Pasadena, Arcadia, Clermont, Covina, El Monte, La Puente, Monrovia, Upland, West Covina)@626
California (Vallejo, Crescent City, Eureka, Redwoods National Park, Santa Rosa, Ukiah)@627
California (San Francisco, Marin County)@628
California (Palo Alto, Menlo Park, Mountain View, San Mateo, Santa Clara, Stanford, South San Francisco)@650
California (Orange County, Anaheim, Fullerton, Garden Grove, Huntington Beach, Irvine, Newport Beach, Santa Ana)@657
California (Bakersfield, Lancaster, Palmdale, Kern County)@661
California (San Jose, Cupertino, Los Gatos, Milpitas, Mountain View, Santa Clara, Sunnyvale, Silicon Valley)@669
California (Vallejo, Crescent City, Eureka, Redwoods National Park, Santa Rosa, Ukiah)@707
California (Orange County, Anaheim, Fullerton, Garden Grove, Huntington Beach, Irvine, Newport Beach, Santa Ana)@714
California (Los Angeles County, San Fernando Valley)@747
California (Bishop, Ridgecrest, San Bernardino County, Riverside County, Palm Springs, Palm Desert) @760
California (Palo Alto, Menlo Park, Mountain View, San Mateo, Santa Clara, Stanford, South San Francisco)@764
California (Ventura County, San Luis Obispo County, Santa Barbara County, Ojai, Oxnard, Santa Maria, Simi Valley)@805
California (San Fernando Valley, Burbank, Encino, Glendale, North Hollywood, San Fernando, Van Nuys, Woodland Hills)@818
California (Monterey, Salinas, Monterey County, Hollister, Santa Cruz)@831
California (San Diego)@858
California (San Bernardino County, Los Angeles County, Riverside County, Fontana, Pomona, Claremont, Chino, Ontario, Redlands)@909
California (Sacramento)@916
California (Livermore, Concord, Pleasant Hill, Walnut Creek, Martinez, Pleasanton, Dublin)@925
California (San Diego, Chula Vista, National City, El Cajon, La Mesa, Lemon Grove)@935
California (Irvine, Lake Forest, California, Newport Beach, Corona del Mar, Orange County)@949
California (Riverside County, Corona, Lake Elsinore, Mira Loma, Moreno Valley, Sun City, Winchester, Lakeview)@951
Colorado@303.719.720.970
Connecticut@203.475.860.959
Delaware@302
District of Columbia DC@202
Florida@239.305.321.352.386.407.561.689.727.754.772.786.813.850.863.904.941.954
Georgia@229.404.470.478.678.706.762.770.912
Hawaii@808
Idaho@208
Illinois@217.224.309.312.331.447.464.618.630.708.730.773.779.815.847.872
Indiana@219.260.317.574.765.812
Iowa@319.515.563.641.712
Kansas@316.620.785.913
Kentucky@270.364.502.606.859
Louisiana@225.318.337.504.985
Maine@207
Maryland@227.240.301.410.443.667
Massachusetts@339.351.413.508.617.774.781.857.978
Michigan@231.248.269.313.517.586.616.679.734.810.906.947.989
Minnesota@218.320.507.612.651.763.952
Mississippi@228.601.662.769
Missouri@314.417.557.573.636.660.816.975
Montana@406
Nebraska@308.402.531
Nevada@702.775
New Hampshire@603
New Jersey@201.551.609.732.848.856.862.908.973
New Mexico@505.575
New York@212.315.347.516.518.585.607.631.646.716.718.845.914.917.929
North Carolina@252.336.704.828.910.919.980.984
North Dakota@701
Ohio@216.234.283.330.380.419.440.513.567.614.740.937
Oklahoma@405.539.580.918
Oregon@458.503.541.971
Pennsylvania@215.267.272.412.445.484.570.582.610.717.724.814.835.878
Rhode Island@401
South Carolina@803.843.864
South Dakota@605
Tennessee@423.615.731.865.901.931
Texas@210.214.254.281.325.361.409.430.432.469.512.682.713.737.806.817.830.832.903.915.936.940.956.972.979
Utah@385.435.801
Vermont@802
Virginia@276.434.540.571.703.757.804
Washington@206.253.360.425.509.564
West Virginia@304.681
Wisconsin@262.274.414.534.608.715.920
Wyoming@307
Alberta@403.587.780.825
British Columbia@236.250.604.672.778
Manitoba@204.431
New Brunswick@506
Newfoundland and Labrador@709
Nova Scotia@902
Ontario@226.249.289.343.365.416.437.519.613.647.705.807.905
Prince Edward Island@902
Quebec@418.438.450.514.579.581.819.873
Saskatchewan@306.639
Yukon Northwest Canada@867
Anguilla@264
Antigua and Barbuda@268
Bahamas@242
Barbados@246
Bermuda@441
British Virgin Islands@284
Cayman Islands@345
Dominica@767
Dominican Republic@809.829.849
Grenada@473
Jamaica@876
Montserrat@664
Puerto Rico@787.939
Saint Kitts and Nevis@869
Saint Lucia@758
Saint Vincent and the Grenadines@784
Sint Maarten@721
Trinidad and Tobago@868
Turks and Caicos Islands@649
US Virgin Islands@340
American Samoa@684
Guam@671
Northern Marianas Islands@670
Canada special services@600
Inbound international@456
Interexchange carrier services@700
Personal Communications Service@500.522.533.544.566.577.588
Premium call services@900
Toll-free@800.822.833.844.855.866.877.880.881.882.888
US government@710
Fictional usage@555
Special code: community social services@211
Special code: city government, non-emergency police@311
Special code: local telephone directory service@411
Special code: traffic, road, tourist information@511
Special code: repair service, or mobile customer service@611
Special code: relay service for hearing or speech disabilities@711
Special code: dig safe underground pipe safety@811
Special code: emergency dispatcher for police, fire, ambulance@911
Special code: returns number@830
Special code: returns number@958
Special code: returns number@998
EOHereDoc
# Type of first argument term determines appropriate action;
# To TEST whether term is an INTEGER, we could use:
# [[ $term =~ ^-?[0-9]+$ ]]
# but double brackets is a bashism,
# and =~ is supported only for bash 3.1 onwards.
#
# awk matches, then PRINTS the appropriate FIELDS.
# -F@ specifies field separator as @ in the phone data above.
#
printsep='"; "'
# Be assured: semi-colons do not appear in the data above,
# which is useful to know for post-processing.
#
if [ "${term//[0-9]*}" = "" ]; then
# yes, natural number -- so print country name:
awk -F@ "\$2 ~ /$term/ {print $term $printsep \$1}" $tmpf > $outf
else
# no, has non-numerical characters -- so print telephone code:
awk -F@ "\$1 ~ /$term/ {print \$2 $printsep \$1}" $tmpf > $outf
fi
# display OUTPUT if non-zero size, otherwise generate error message:
# remember to clean-up...
if [ -s $outf ] ; then
cat $outf
rm -f $tmpf $outf
else
echo " !! ${0##*/}: NULL response; check arguments and data." 1>&2
rm -f $tmpf $outf
exit 113
fi
exit 0
# _______________ EOS :: END of Script ::::::::::::::::::::::::::::::::::::::::
# Footnote: there are also SPECIAL CODES, such as:
#
# *51 and 1151 A history of unanswered calls on a telephone number.
# *57 and 1157 Trace harassing, threatening, abusive, obscene calls.
# *66 and 1166 Keep retrying a busy-line (Called-party camp-on).
# *67 and 1167 Caller ID Block.
# *69 and 1169 Call Return.
# *70 and 1170 Cancel call waiting on a call-by-call basis.
# *71 and 1171 Three way calling.
# *74 and 1174 Speed dialing.
# *75 allows a total of 30 speed-call numbers.
# *82 and 1182 Releases Caller ID block on a call-by-call basis.
#
# Note: The four digit numbers do not work in some areas. The codes prefixed
# with the "*" symbol are intended for use on Touch-Tone telephones, whereas the
# four-digit numbers prefixed 11xx are intended for use on older rotary dial
# telephones,
# StackOverflow: Test whether bash string is valid as an integer:
# http://stackoverflow.com/questions/2210349/
# vim: set fileencoding=utf-8 ff=unix tw=78 ai syn=sh :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment