#! /usr/bin/gawk -f # Last edited on 2001-09-17 19:52:42 by stolfi BEGIN { abort = -1; usage = "name-to-sex < INFILE > OUTFILE"; tblfile = "${STOLFIHOME}/lib/name-to-sex.tbl"; # Read a file where the first field in each line is name, with # Up-and-lows capitalization, and normalized diacritics. # Outputs the same file where each line is prefixed with # the sex ("M", "F", or "?"), as deduced from the name. # Read table into "sex" array: read_sex(tblfile); } /./ { name = $1; if (name in sex) { s = sex[name]; } else { s = "?"; } printf "%s %s\n", s, $0; next; } function read_sex(tblfile, nMap,lin,fld,nfld) { split("", sex); nMap = 0; while((getline lin < tblfile) > 0) { nMap++; if (! match(lin, /^ *([#]|$)/)) { nfld = split(lin, fld, " "); if (nfld != 2) tbl_error(tblfile, nMap, ("bad table entry = \"" lin "\"")); if (fld[1] in sex) tbl_error(tblfile, nMap, ("repeated key = \"" lin "\"")); if (fld[2] !~ /^[MF?]$/) tbl_error(tblfile, nMap, ("bad sex = \"" lin "\"")); sex[fld[1]] = fld[2]; } } if (ERRNO != "0") { tbl_error(tblfile, nMap,(tblfile ": " ERRNO)); } close (tblfile); if (nMap == 0) { arg_error(("file \"" tblfile "\" empty or missing")); } # printf "loaded %6d map pairs\n", nMap > "/dev/stderr" } function arg_error(msg) { printf "%s\n", msg >> "/dev/stderr"; printf "usage: %s\n", usage >> "/dev/stderr"; abort = 1; exit 1 } function tbl_error(tblfile, nMap, msg) { printf "%s, line %d: %s\n", tblfile, nMap, msg >> "/dev/stderr"; abort = 1; exit 1 } function data_error(msg) { printf "line %d: %s\n", NR, msg >> "/dev/stderr"; abort = 1; exit 1 }