#!/bin/sh # # Prefix -> Country lookup utility # (c) 1997 Mats Petersson SM5SXL # search_for_match() { awk "BEGIN { FS = \"\t\" num_found = 0 prefix = ( \":\" substr(\"$PREFIX\", 1, $LEN) \":\" ) cf = \"/home/mats/datafiles/country.dat\" } \$2 ~ prefix { cntry = \$1 while(getline < cf) { if(\$1 == cntry) cntry_arr[num_found++] = \$1 } close(cf) } END { if (! num_found) exit 0 if(num_found > 1) { printf(\"\n\") > \"/dev/stderr\" for(i = 0; i < num_found; i++) printf(\"(%d) %s \", i + 1, cntry_arr[i]) > \"/dev/stderr\" printf(\"\n\nWhich country? \") > \"/dev/stderr\" getline n < \"/dev/stdin\" n-- print cntry_arr[n] } else print cntry exit 1 } " ~/datafiles/prefix.dat } if [ $# = 0 ]; then echo "usage: pfx prefix" >&2 exit 1 fi PREFIX=$1 LEN=$(echo -n $PREFIX | wc -c) if [ $LEN -gt 4 ]; then LEN=4 fi while search_for_match; do LEN=$[ $LEN - 1 ] if [ $LEN = 0 ]; then echo No match found! exit fi done .