#!/bin/sh # # Prefix -> Country lookup utility # (c) 1997 Mats Petersson SM5SXL # search_for_match() { awk "BEGIN { FS = \"\t\" found = 0 num_found = 0 full_report = $FULLREP prefix = ( \":\" substr(\"$PREFIX\", 1, $LEN) \":\" ) } { if(\$2 ~ prefix) { cntry = \$1 while(getline < \"/home/mats/datafiles/country.dat\") { if(\$1 == cntry) { if(full_report) { printf(\"\nPrefix: %s\n\ Country: %s\nContinent: %s\nITU Zone: %s\nCQ Zone: %s\nTime Zone: %s\n\ Latitude: %s\n\Longitude: %s\n\n\", substr(prefix, 2, $LEN), \$1, \$2, \$3,\ \$4, \$5, \$6, \$7) } else { cntry_arr[num_found] = \$1 num_found++ } found = 1 } } close(\"/home/mats/datafiles/country.dat\") } } END { if (! found) exit 0 if(full_report) exit 1 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 [ $# -lt 2 ]; then echo "usage: pfx prefix full_report" >&2 exit 1 fi PREFIX=$1 FULLREP=$2 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 .