August 2, 2024

Look up U.S. Zip codes from the command line offline

Intro

The aim is to be able to type:

$ zipcode brooklyn

and get:

Found 1 items, similar to brooklyn.
-->quick_plz.us
-->Brooklyn

06234, 11201 NY (+1 718), 11202 NY (+1 718), 11203 NY (+1 718), 11204 NY (+1 718), 11205 NY (+1 718), 11206 NY (+1 718), 11207 NY (+1 718), 11208 NY (+1 718), 11209 NY (+ 1 718), 11210 NY (+1 718), 11211 NY (+1 718), 11212 NY (+1 718), 11213 NY (+1 718), 11214 NY (+1 718), 11215 NY (+1 718), 11216 NY (+1 718), 11217 NY (+1 718), 11218 NY ( +1 718), 11219 NY (+1 718), 11220 NY (+1 718), 11221 NY (+1 718), 11222 NY (+1 718), 11223 NY (+1 718), 11224 NY (+1 718), 11225 NY (+1 718), 11226 NY (+1 718), 11228 NY (+1 718), 11229 NY (+1 718), 11230 NY (+1 718), 11231 NY (+1 718), 11232 NY (+1 718), 11233 NY (+1 718), 11234 NY (+1 718), 11235 NY (+1 718), 11236 NY (+1 718), 11237 NY (+1 718), 11238 NY (+1 718), 11239 NY (+1 718), 11240 NY (+1 718), 11241 NY (+1 718), 11242 NY (+1 718), 11243 NY (+1 718), 11244 NY (+1 718), 11245 NY (+1 718), 11247 N Y (+1 718), 11248 NY (+1 718), 11249 NY (+1 718), 11251 NY (+1 718), 11252 NY (+1 718), 11254 NY (+1 718), 11255 NY (+1 718), 11256 NY (+1 718), 18813 PA (+1 717), 21225 MD (+1 410), 36429 AL (+1 334), 39425 MS (+1 601), 42209 KY (+1 502), 46111 IN (+1 317), 49230 MI (+1 517), 52211 IA (+1 515), 53521 WI (+1 608)

Set up the programme sdcv

You will use sdcv to query the file containing zip codes and display them.

To install sdcv, run:

sudo apt install sdcv

Download the file with zip codes

You need to find a dictionary which contains the zip code file. The one found I found is called quick_plz.us.

It is part of the dictionary called quick_eng-eng which (at the time of writing) you can download from there: https://web.archive.org/web/20140428004044/http://abloz.com/huzheng/stardict-dic/Quick/

Create a directory where you’ll place the zip code file

Once you have downloaded the file, create a directory where sdcv will look for dictionaries:

sudo mkdir -p /usr/share/stardict/dic/  

Extract and move the file with zip codes to the directory

Then download the programme you will need to extract dictionary files (i.e. tar):

sudo apt install tar 

Next extract the files from the file you’ve downloaded and place them into the directory you created. We do these two actions with one single command:

sudo tar -xvjf downloaded.tar.bz2 -C /usr/share/stardict/dic

Note: you can check the man page of tar to see what these options are about $ man tar.

Now sdcv should be able to query the file containing zip codes quick_plz.us.

Test

To test, run:

$ sdcv -u 'quick_plz.us' brooklyn

You should get:

Found 1 items, similar to brooklyn.
-->quick_plz.us
-->Brooklyn

06234, 11201 NY (+1 718), 11202 NY (+1 718), 11203 NY (+1 718), 11204 NY (+1 718), 11205 NY (+1 718), 11206 NY (+1 718), 11207 NY (+1 718), 11208 NY (+1 718), 11209 NY (+ 1 718), 11210 NY (+1 718), 11211 NY (+1 718), 11212 NY (+1 718), 11213 NY (+1 718), 11214 NY (+1 718), 11215 NY (+1 718), 11216 NY (+1 718), 11217 NY (+1 718), 11218 NY ( +1 718), 11219 NY (+1 718), 11220 NY (+1 718), 11221 NY (+1 718), 11222 NY (+1 718), 11223 NY (+1 718), 11224 NY (+1 718), 11225 NY (+1 718), 11226 NY (+1 718), 11228 NY (+1 718), 11229 NY (+1 718), 11230 NY (+1 718), 11231 NY (+1 718), 11232 NY (+1 718), 11233 NY (+1 718), 11234 NY (+1 718), 11235 NY (+1 718), 11236 NY (+1 718), 11237 NY (+1 718), 11238 NY (+1 718), 11239 NY (+1 718), 11240 NY (+1 718), 11241 NY (+1 718), 11242 NY (+1 718), 11243 NY (+1 718), 11244 NY (+1 718), 11245 NY (+1 718), 11247 N Y (+1 718), 11248 NY (+1 718), 11249 NY (+1 718), 11251 NY (+1 718), 11252 NY (+1 718), 11254 NY (+1 718), 11255 NY (+1 718), 11256 NY (+1 718), 18813 PA (+1 717), 21225 MD (+1 410), 36429 AL (+1 334), 39425 MS (+1 601), 42209 KY (+1 502), 46111 IN (+1 317), 49230 MI (+1 517), 52211 IA (+1 515), 53521 WI (+1 608)

Make an alias called zip code

Next step is to make an alias so instead of typing:

sdcv -u 'quick_plz.us' brooklyn

you only have to type:

zipcode brooklyn

To create an alias run:

$ cd
$ echo "alias zipcode='sdcv -u 'quick_plz.us' brooklyn'" >> .bash_aliases

Once you’ve created the alias, run:

bash

for your shell (terminal) to reload your configuration files as well as the file containing the aliases.

Test:

$ zipcode brooklyn

Credit for the section on adding a dictionary to sdcv: green (askubuntu)

You might be interested in the following wikis:

  • https://yctct.com/sdcv-add-dic; in this post, I explain: where to find dictionaries, and where to extract the compressed files you’ll have downloaded so that sdcv can query them.
  • once you have dictionaries in a directory where sdcv can find them, you will notice that the output of sdcv can be longer than one screen. If you want to add a function which passes the output of sdcv, see https://yctct.com/sdcv-pass-output-less.

personal computing command-line interface (cli) gnu linux trisquel shell literacy office applications wiki sdcv offline

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88