The Adapter in AWK
Mutate legacy fixed-width text streams into modern JSON sigils.
✧ GANG OF FOUR LINEAGE & ARCANUM ✧
GOF DESIGN PATTERN: Adapter
ARCANUM SCHOOL: Transmutation // Format-Shifting
SCRIBE / ARCHON: Joshua Edward McLaughlin Cox
CANONICAL REGISTRATION:
// CODEX INCANTATION //
# Adapting CSV/TSV positional fields to a JSON output structure
function adapt_row_to_json(f1, f2, f3) {
return sprintf("{\"id\": \"%s\", \"spell\": \"%s\", \"cost\": \"%s\"}", f1, f2, f3)
}
BEGIN {
FS=","
print "["
}
# Each record is intercepted and adapted
NR > 1 {
print adapt_row_to_json($1, $2, $3) ","
}
END {
print "]"
}
The core identity of AWK is an Adapter. It continuously intercepts streams of unstructured or semi-structured data and maps them into another interface format. Here, we encapsulate the adaptation logic into a precise transmutation function.
Runic Tags:
#awk
#text-processing
#structural