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