#! /usr/bin/gawk -f BEGIN { alpha[0] = "0"; alpha[1] = "1"; alpha[2] = "ABC"; alpha[3] = "DEF"; alpha[4] = "GHI"; alpha[5] = "JKL"; alpha[6] = "MNO"; alpha[7] = "PQRS"; alpha[8] = "TUV"; alpha[9] = "WXYZ"; } /[^0-9\- ]/ { print "Bad input line " NR > "/dev/stderr" ; next } { gsub(/[\- ]+/, ""); split($0, digits, ""); Bag[""] = 1; for (i=1; i<=length($0); i++) { split(alpha[digits[i]], temp, ""); for (s in Bag) { if (length(s)==i-1) { for (k in temp) { Bag[s temp[k]] = 1; } } } } # output stuff printf "%s:\n", $0; for (s in Bag) { if (length(s)==length($0)) { printf "\t%s\n", s | "sort"; } } close("sort"); # be ready for next input delete Bag; }