Skip to content

Instantly share code, notes, and snippets.

@fitnr
Last active July 31, 2022 01:01
Show Gist options
  • Save fitnr/dca3aa637e7b5fed3f05 to your computer and use it in GitHub Desktop.
Save fitnr/dca3aa637e7b5fed3f05 to your computer and use it in GitHub Desktop.
Replace US states with their FIPS code or postal code or vis/versa
#!/bin/bash
# Replace FIPS codes with US states/^territory names
# usage: sh fips2state2.sh inputfile.txt > outputfile.txt
# or just copy everything but the '$1' and run on the command line
sed -e 's/^54/West Virginia/' -e 's/^02/Alaska/' -e 's/^01/Alabama/' -e 's/^05/Arkansas/' -e 's/^60/American Samoa/' \
-e 's/^04/Arizona/' -e 's/^06/California/' -e 's/^08/Colorado/' -e 's/^09/Connecticut/' -e 's/^11/District of Columbia/' \
-e 's/^10/Delaware/' -e 's/^12/Florida/' -e 's/^13/Georgia/' -e 's/^66/Guam/' -e 's/^15/Hawaii/' \
-e 's/^19/Iowa/' -e 's/^16/Idaho/' -e 's/^17/Illinois/' -e 's/^18/Indiana/' -e 's/^20/Kansas/' \
-e 's/^21/Kentucky/' -e 's/^22/Louisiana/' -e 's/^25/Massachusetts/' -e 's/^24/Maryland/' -e 's/^23/Maine/' \
-e 's/^26/Michigan/' -e 's/^27/Minnesota/' -e 's/^29/Missouri/' -e 's/^28/Mississippi/' -e 's/^30/Montana/' \
-e 's/^37/North Carolina/' -e 's/^38/North Dakota/' -e 's/^31/Nebraska/' -e 's/^33/New Hampshire/' -e 's/^34/New Jersey/' \
-e 's/^35/New Mexico/' -e 's/^32/Nevada/' -e 's/^36/New York/' -e 's/^39/Ohio/' -e 's/^40/Oklahoma/' \
-e 's/^41/Oregon/' -e 's/^42/Pennsylvania/' -e 's/^72/Puerto Rico/' -e 's/^44/Rhode Island/' -e 's/^45/South Carolina/' \
-e 's/^46/South Dakota/' -e 's/^47/Tennessee/' -e 's/^48/Texas/' -e 's/^49/Utah/' -e 's/^51/Virginia/' \
-e 's/^78/Virgin Islands/' -e 's/^50/Vermont/' -e 's/^53/Washington/' -e 's/^55/Wisconsin/' -e 's/^56/Wyoming/' $1
#!/bin/bash
# Replace the postal appreviations with the full name for US and Canadian states, provinces and territories.
# Usage sh ./postal2state.sh inputfile.txt > outputfile.txt
# or just copy everything but the '$1' and run on the command line
sed -e 's/[[:<:]]AK[[:>:]]/Alaska/g' -e 's/[[:<:]]AL[[:>:]]/Alabama/g' -e 's/[[:<:]]AR[[:>:]]/Arkansas/g' -e 's/[[:<:]]AS[[:>:]]/American Samoa/g' -e 's/[[:<:]]AZ[[:>:]]/Arizona/g' -e 's/[[:<:]]CA[[:>:]]/California/g' -e 's/[[:<:]]CO[[:>:]]/Colorado/g' -e 's/[[:<:]]CT[[:>:]]/Connecticut/g' -e 's/[[:<:]]DC[[:>:]]/District of Columbia/g' -e 's/[[:<:]]DE[[:>:]]/Delaware/g' \
-e 's/[[:<:]]FL[[:>:]]/Florida/g' -e 's/[[:<:]]GA[[:>:]]/Georgia/g' -e 's/[[:<:]]GU[[:>:]]/Guam/g' -e 's/[[:<:]]HI[[:>:]]/Hawaii/g' -e 's/[[:<:]]IA[[:>:]]/Iowa/g' -e 's/[[:<:]]ID[[:>:]]/Idaho/g' -e 's/[[:<:]]IL[[:>:]]/Illinois/g' -e 's/[[:<:]]IN[[:>:]]/Indiana/g' -e 's/[[:<:]]KS[[:>:]]/Kansas/g' -e 's/[[:<:]]KY[[:>:]]/Kentucky/g' \
-e 's/[[:<:]]LA[[:>:]]/Louisiana/g' -e 's/[[:<:]]MA[[:>:]]/Massachusetts/g' -e 's/[[:<:]]MD[[:>:]]/Maryland/g' -e 's/[[:<:]]ME[[:>:]]/Maine/g' -e 's/[[:<:]]MI[[:>:]]/Michigan/g' -e 's/[[:<:]]MN[[:>:]]/Minnesota/g' -e 's/[[:<:]]MO[[:>:]]/Missouri/g' -e 's/[[:<:]]MS[[:>:]]/Mississippi/g' -e 's/[[:<:]]MT[[:>:]]/Montana/g' -e 's/[[:<:]]NC[[:>:]]/North Carolina/g' \
-e 's/[[:<:]]ND[[:>:]]/North Dakota/g' -e 's/[[:<:]]NE[[:>:]]/Nebraska/g' -e 's/[[:<:]]NH[[:>:]]/New Hampshire/g' -e 's/[[:<:]]NJ[[:>:]]/New Jersey/g' -e 's/[[:<:]]NM[[:>:]]/New Mexico/g' -e 's/[[:<:]]NV[[:>:]]/Nevada/g' -e 's/[[:<:]]NY[[:>:]]/New York/g' -e 's/[[:<:]]OH[[:>:]]/Ohio/g' -e 's/[[:<:]]OK[[:>:]]/Oklahoma/g' -e 's/[[:<:]]OR[[:>:]]/Oregon/g' \
-e 's/[[:<:]]PA[[:>:]]/Pennsylvania/g' -e 's/[[:<:]]PR[[:>:]]/Puerto Rico/g' -e 's/[[:<:]]RI[[:>:]]/Rhode Island/g' -e 's/[[:<:]]SC[[:>:]]/South Carolina/g' -e 's/[[:<:]]SD[[:>:]]/South Dakota/g' -e 's/[[:<:]]TN[[:>:]]/Tennessee/g' -e 's/[[:<:]]TX[[:>:]]/Texas/g' -e 's/[[:<:]]UT[[:>:]]/Utah/g' -e 's/[[:<:]]VA[[:>:]]/Virginia/g' -e 's/[[:<:]]VI[[:>:]]/Virgin Islands/g' \
-e 's/[[:<:]]VT[[:>:]]/Vermont/g' -e 's/[[:<:]]WA[[:>:]]/Washington/g' -e 's/[[:<:]]WI[[:>:]]/Wisconsin/g' -e 's/[[:<:]]WV[[:>:]]/West Virginia/g' -e 's/[[:<:]]WY[[:>:]]/Wyoming/g' -e 's/[[:<:]]AB[[:>:]]/Alberta/g' -e 's/[[:<:]]BC[[:>:]]/British Columbia/g' -e 's/[[:<:]]MB[[:>:]]/Manitoba/g' -e 's/[[:<:]]NB[[:>:]]/New Brunswick/g' -e 's/[[:<:]]NL[[:>:]]/Newfoundland and Labrador/g' \
-e 's/[[:<:]]NS[[:>:]]/Nova Scotia/g' -e 's/[[:<:]]NT[[:>:]]/Northwest Territories/g' -e 's/[[:<:]]NU[[:>:]]/Nunavut/g' -e 's/[[:<:]]ON[[:>:]]/Ontario/g' -e 's/[[:<:]]PE[[:>:]]/Prince Edward Island/g' -e 's/[[:<:]]QC[[:>:]]/Quebec/g' -e 's/[[:<:]]SK[[:>:]]/Saskatchewan/g' -e 's/[[:<:]]YT[[:>:]]/Yukon/g' $1
#!/bin/bash
# Replace US states & territories with their FIPS code.
# usage: sh state2fips.sh inputfile.txt > outputfile.txt
# or just copy everything but the '$1' and run on the command line
sed -e 's/[Ww]est [Vv]irginia/54/g' -e 's/[Aa]laska/02/g' -e 's/[Aa]labama/01/g' -e 's/[Aa]rkansas/05/g' -e 's/[Aa]merican [Ss]amoa/60/g' \
-e 's/[Aa]rizona/04/g' -e 's/[Cc]alifornia/06/g' -e 's/[Cc]olorado/08/g' -e 's/[Cc]onnecticut/09/g' -e 's/[Dd]istrict [Oo]f [Cc]olumbia/11/g' \
-e 's/[Dd]elaware/10/g' -e 's/[Ff]lorida/12/g' -e 's/[Gg]eorgia/13/g' -e 's/[Gg]uam/66/g' -e 's/[Hh]awaii/15/g' \
-e 's/[Ii]owa/19/g' -e 's/[Ii]daho/16/g' -e 's/[Ii]llinois/17/g' -e 's/[Ii]ndiana/18/g' -e 's/[Kk]ansas/20/g' \
-e 's/[Kk]entucky/21/g' -e 's/[Ll]ouisiana/22/g' -e 's/[Mm]assachusetts/25/g' -e 's/[Mm]aryland/24/g' -e 's/[Mm]aine/23/g' \
-e 's/[Mm]ichigan/26/g' -e 's/[Mm]innesota/27/g' -e 's/[Mm]issouri/29/g' -e 's/[Mm]ississippi/28/g' -e 's/[Mm]ontana/30/g' \
-e 's/[Nn]orth [Cc]arolina/37/g' -e 's/[Nn]orth [Dd]akota/38/g' -e 's/[Nn]ebraska/31/g' -e 's/[Nn]ew [Hh]ampshire/33/g' -e 's/[Nn]ew [Jj]ersey/34/g' \
-e 's/[Nn]ew [Mm]exico/35/g' -e 's/[Nn]evada/32/g' -e 's/[Nn]ew [Yy]ork/36/g' -e 's/[Oo]hio/39/g' -e 's/[Oo]klahoma/40/g' \
-e 's/[Oo]regon/41/g' -e 's/[Pp]ennsylvania/42/g' -e 's/[Pp]uerto [Rr]ico/72/g' -e 's/[Rr]hode [Ii]sland/44/g' -e 's/[Ss]outh [Cc]arolina/45/g' \
-e 's/[Ss]outh [Dd]akota/46/g' -e 's/[Tt]ennessee/47/g' -e 's/[Tt]exas/48/g' -e 's/[Uu]tah/49/g' -e 's/[Vv]irginia/51/g' \
-e 's/[Vv]irgin [Ii]slands/78/g' -e 's/[Vv]ermont/50/g' -e 's/[Ww]ashington/53/g' -e 's/[Ww]isconsin/55/g' -e 's/[Ww]yoming/56/g' $1
#!/bin/bash
# Replace US States & Territories, Canadian Provinces & Territories with postal abbreviations
# usage: sh state2postal.sh inputfile.txt > outputfile.txt
# or just copy everything but the '$1' and run on the command line
sed -e 's/[Ww]est [Vv]irginia/WV/g' -e 's/[Aa]laska/AK/g' -e 's/[Aa]labama/AL/g' -e 's/[Aa]rkansas/AR/g' -e 's/[Aa]merican [Ss]amoa/AS/g' -e 's/[Aa]rizona/AZ/g' -e 's/[Cc]alifornia/CA/g' -e 's/[Cc]olorado/CO/g' -e 's/[Cc]onnecticut/CT/g' -e 's/[Dd]istrict [Oo]f [Cc]olumbia/DC/g' \
-e 's/[Dd]elaware/DE/g' -e 's/[Ff]lorida/FL/g' -e 's/[Gg]eorgia/GA/g' -e 's/[Gg]uam/GU/g' -e 's/[Hh]awaii/HI/g' -e 's/[Ii]owa/IA/g' -e 's/[Ii]daho/ID/g' -e 's/[Ii]llinois/IL/g' -e 's/[Ii]ndiana/IN/g' -e 's/[Kk]ansas/KS/g' \
-e 's/[Kk]entucky/KY/g' -e 's/[Ll]ouisiana/LA/g' -e 's/[Mm]assachusetts/MA/g' -e 's/[Mm]aryland/MD/g' -e 's/[Mm]aine/ME/g' -e 's/[Mm]ichigan/MI/g' -e 's/[Mm]innesota/MN/g' -e 's/[Mm]issouri/MO/g' -e 's/[Mm]ississippi/MS/g' -e 's/[Mm]ontana/MT/g' \
-e 's/[Nn]orth [Cc]arolina/NC/g' -e 's/[Nn]orth [Dd]akota/ND/g' -e 's/[Nn]ebraska/NE/g' -e 's/[Nn]ew [Hh]ampshire/NH/g' -e 's/[Nn]ew [Jj]ersey/NJ/g' -e 's/[Nn]ew [Mm]exico/NM/g' -e 's/[Nn]evada/NV/g' -e 's/[Nn]ew [Yy]ork/NY/g' -e 's/[Oo]hio/OH/g' -e 's/[Oo]klahoma/OK/g' \
-e 's/[Oo]regon/OR/g' -e 's/[Pp]ennsylvania/PA/g' -e 's/[Pp]uerto [Rr]ico/PR/g' -e 's/[Rr]hode [Ii]sland/RI/g' -e 's/[Ss]outh [Cc]arolina/SC/g' -e 's/[Ss]outh [Dd]akota/SD/g' -e 's/[Tt]ennessee/TN/g' -e 's/[Tt]exas/TX/g' -e 's/[Uu]tah/UT/g' -e 's/[Vv]irginia/VA/g' \
-e 's/[Vv]irgin [Ii]slands/VI/g' -e 's/[Vv]ermont/VT/g' -e 's/[Ww]ashington/WA/g' -e 's/[Ww]isconsin/WI/g' -e 's/[Ww]yoming/WY/g' \
-e 's/Alberta/AB/g' -e 's/British Columbia/BC/g' -e 's/Manitoba/MB/g' -e 's/New Brunswick/NB/g' -e 's/Newfoundland and Labrador/NL/g' \
-e 's/Nova Scotia/NS/g' -e 's/Northwest Territories/NT/g' -e 's/Nunavut/NU/g' -e 's/Ontario/ON/g' -e 's/Prince Edward Island/PE/g' \
-e 's/Quebec/QC/g' -e 's/Saskatchewan/SK/g' -e 's/Yukon/YT/g' $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment