sxiii

Script To Parse ZIP+4 US Addresses into SQL file

Mar 17th, 2013
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.02 KB | None | 0 0
  1. #!/bin/bash
  2. # This script parses 4-ZIP US database from input text file (add text file name when running)
  3. # and creates an SQL requests which you can execute directly and get a MySQL or otherSQL
  4. # database of the US addresses. Run like: ./us-postal-parser.sh inputfile.txt, for ex. with demodata:
  5. # /us-postal-parser.sh 979.txt
  6. # Written according this specs: http://www.zipinfo.com/products/natzip4/natzip4.htm
  7. # Written for the following task: http://freelance.ru/discussion/?cmd=msg_new&topic=215400#m_form
  8. # Written by Security XIII [at gmail dot com] @ 17.03.2013
  9.  
  10. # Please correct this values according to your needs:
  11.  
  12. TABLENAME=demotable       # Put your tablename here
  13. RESULTFILE=big-parsed.sql # Put your sql resulting filename here
  14. RECORDLEN=182
  15.  
  16. ##############################################################################################
  17. ################## It is not recommended to edit below this line. ############################
  18. ##############################################################################################
  19.  
  20. while read -n $RECORDLEN line; do
  21.  
  22. echo "INSERT INTO $TABLENAME VALUES ( 'zip','${line:0:5}' )" >> $RESULTFILE
  23. echo "INSERT INTO $TABLENAME VALUES ( 'rectype','${line:5:1}' )" >> $RESULTFILE
  24. echo "INSERT INTO $TABLENAME VALUES ( 'carrier','${line:6:4}' )" >> $RESULTFILE
  25. echo "INSERT INTO $TABLENAME VALUES ( 'streetpredir','${line:10:2}' )" >> $RESULTFILE
  26. echo "INSERT INTO $TABLENAME VALUES ( 'streetname','${line:12:28}' )" >> $RESULTFILE
  27. echo "INSERT INTO $TABLENAME VALUES ( 'streetsuffix','${line:40:4}' )" >> $RESULTFILE
  28. echo "INSERT INTO $TABLENAME VALUES ( 'streetpostdir','${line:44:2}' )" >> $RESULTFILE
  29. echo "INSERT INTO $TABLENAME VALUES ( 'primarylow','${line:46:10}' )" >> $RESULTFILE
  30. echo "INSERT INTO $TABLENAME VALUES ( 'primaryhigh','${line:56:10}' )" >> $RESULTFILE
  31. echo "INSERT INTO $TABLENAME VALUES ( 'primaryside','${line:66:1}' )" >> $RESULTFILE
  32. echo "INSERT INTO $TABLENAME VALUES ( 'buildingname','${line:67:40}' )" >> $RESULTFILE
  33. echo "INSERT INTO $TABLENAME VALUES ( 'secondarytype','${line:107:4}' )" >> $RESULTFILE
  34. echo "INSERT INTO $TABLENAME VALUES ( 'secondarylow','${line:111:8}' )" >> $RESULTFILE
  35. echo "INSERT INTO $TABLENAME VALUES ( 'secondaryhigh','${line:119:8}' )" >> $RESULTFILE
  36. echo "INSERT INTO $TABLENAME VALUES ( 'secondaryside','${line:127:1}' )" >> $RESULTFILE
  37. echo "INSERT INTO $TABLENAME VALUES ( 'zip4addrlow','${line:128:4}' )" >> $RESULTFILE
  38. echo "INSERT INTO $TABLENAME VALUES ( 'zip4addrhigh','${line:132:4}' )" >> $RESULTFILE
  39. echo "INSERT INTO $TABLENAME VALUES ( 'statecode','${line:136:2}' )" >> $RESULTFILE
  40. echo "INSERT INTO $TABLENAME VALUES ( 'countyfipscode','${line:138:3}' )" >> $RESULTFILE
  41. echo "INSERT INTO $TABLENAME VALUES ( 'congressdistrict','${line:141:2}' )" >> $RESULTFILE
  42. echo "INSERT INTO $TABLENAME VALUES ( 'citystatekey','${line:143:6}' )" >> $RESULTFILE
  43.  
  44. done < $1
  45.  
  46. # For instance:
  47. # ZIP Code          5       5-digit ZIP code
  48. # Record Type           1       Firm, High-rise, PO Box, Street, Rural route, etc.
  49. # Carrier route ID      4      
  50. # Street predirection       2       N, S, E, W, NE, NW, SE, SW
  51. # Street name           28     
  52. # Street suffix         4       ST, RD, DR, LN, etc.
  53. # Street postdirection      2       N, S, E, W, NE, NW, SE, SW
  54. # Primary low address       10      usually numeric
  55. # Primary high address      10      usually numeric
  56. # Primary even/odd/both     1       even or odd numbers, or both
  57. # Building/firm name        40      Primarily firm or high-rise record types
  58. # Secondary address type    4       APT, STE, BLDG, etc.
  59. # Secondary low address     8       usually numeric
  60. # Secondary high address    8       usually numeric
  61. # Secondary even/odd/both   1       even or odd numbers, or both
  62. # ZIP+4 add on low      4       numeric
  63. # ZIP+4 add on high         4       numeric
  64. # State code            2       CA, NY, etc.
  65. # County FIPS code      3       Federal Information Processing Standard code
  66. # Congressional district    2       numeric
  67. # City-state key        6       Preferred city name record in the city-state file
  68. # Stricture of inputfile: no-delimeters, each record is 182 bytes long
  69. # Structure of script: # ${[variableName]:[startIndex]:[length]}
Advertisement
Add Comment
Please, Sign In to add comment