I'm a postal worker (mail sorter) and am trying to write a bash script which allows me to input an exact street address and first couple letters of a street name, and have it return the matched string which contains the route number information. I have to sort thousands of letters which can't be machined per day by looking down at the letter, then looking up at massive posters with every address in my city on them. This script could save me literal hours, so I'm trying my hardest to learn the process to get it done. I have a hobby-like background in unix/linux scripting. Not sure if regex is the solution here, or variations of grep, find, awk, sed, or all of them!
I have a text file with a list of addresses (house number ranges and street names), each on newlines, like such:
6974-7075 hwy 99: ss1 7757-8079 hwy 99: ss14 98-258 even foo st N: 15 97-257 odd foo st N: 16 21-301 foo st S: 17 15-20 foo st S: 7 bar st: 1 fake st: 31 fake pl: 77 sample dr: 89
Notice the existance of number ranges, streets with one route (no house #'s given), even and odd specifiers, road type (st, hwy, pl, dr, etc.), north(N) and south(S) indicators, and finally route info after a colon.
I have the following script that can return the strings I want as long as I input a street number which appears exactly as is in the text file:
#! /bin/bash civic="$1" street="$2" grep $civic.*$street /path/to/addresses.txt
./script.sh 7757 or
./script.sh 7757 h will return
7757-8079 hwy 99: ss14 for me. I like the full string returned here, not just the route after the colon. But, obviously, executing
./script.sh 8020 h won't return
7757-8079 hwy 99: ss14, as my code doesn't check for numbers within a range yet.
However, I'm looking for a way to be able to enter
8020 h and still have it return
7757-8079 hwy 9: ss14, as 8020 is within the range 7757-8079.
Furthermore, notice foo st in the text has an even and odd range with different routes, as well as N and S indicators. I'm looking for a way to be able to enter
107 f or
107 foo and have it return
97-257 odd foo st N: 16, without also returning
98-258 even foo st N: 15, as the house # is odd. The words even/odd will always be specified in the strings for those even/odd cases, so maybe a grep or search for those words in a string with a number range if the house # entered is odd? That example would also return
21-301 odd foo st S: 17 (notice foo st S) as the the house # is within the range, and the string has odd in it. I'm ok with this returning both N and S, as I won't have time to specify N or S.
I'd be extremely grateful for any help offered in my endeavor, whether a fully fleshed out answer, or a hint at getting closer. Not here to mooch, but simply ask for some help! Let me know if I can be more specific.