#! /usr/bin/gawk -f # Last edited on 2022-06-25 23:03:20 by stolfi BEGIN{ abort = -1; split("", ref) nref = 0 } (abort >= 0) { exit abort; } //{ # Extract references, store in {ref[0..nref-1]}: lin = $0 # printf "lin = [%s]\n", lin > "/dev/stderr" new = "" while(match(lin,/\/]+>/)) { # Extract the key: pre = substr(lin, 1, RSTART-1) key = substr(lin, RSTART, RLENGTH) lin = substr(lin, RSTART+RLENGTH) rkey = key; gsub(/ *>/, "/>", rkey) new = (new pre rkey) # printf "\n" > "/dev/stderr" # printf "new = [%s]\n", new > "/dev/stderr" # printf "lin = [%s]\n", lin > "/dev/stderr" # Extract the body: if (! match(lin, /< *[\/] *ref *>/)) { data_error("missing "); } bdy = substr(lin, 1, RSTART-1) lin = substr(lin, RSTART+RLENGTH) # printf "\n" > "/dev/stderr" # printf "bdy = [%s]\n", bdy > "/dev/stderr" # printf "lin = [%s]\n", lin > "/dev/stderr" ref[nref] = (key bdy "") nref++ } new = (new lin) lin = "" # printf "\n" > "/dev/stderr" # printf "new = [%s]\n", new > "/dev/stderr" print new next } END { if (abort >= 0) { exit(abort); } for (i = 0; i < nref; i++) { print ref[i]; print "" } } function data_error(msg) { printf "%s:%d: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; abort = 1; exit 1 }