# print_wrap() will break long lines into line continuations
function print_wrap(str, len) {
line = 1
buf = str
while (length(buf) > len) {
chunk = substr(buf, 1, len)
if (match(chunk, / [^ ]*$/)) {
before = substr(buf, 1, RSTART-1)
after = substr(buf, RSTART+1)
if (line == 1) {
print before
} else {
print " " before
}
buf = after
} else {
break
}
line++
}
if (line == 1) {
print buf
} else {
print " " buf
}
}
BEGIN {
FS = "\t"
delete seen
delete header
}
# process TSV header
NR == 1 {
for (i = 1; i <= NF; i++) {
name = $i
if (seen[name] > 0) {
print "Error: Duplicate field name \"" name "\""
exit -1
}
seen[name] = 1
header[i] = name
}
}
# process each TSV row
NR > 1 {
for (i = 1; i <= NF; i++) {
if (length($i) == 0) {
# name only, empty value
print header[i] ":"
} else if ($i ~ /\\[nrt]/) {
# name and block value
printf "%s: \\\n%s\n", header[i], indent(decode($i))
} else {
# name and value, possibly with line continuations
line = header[i] ": " $i
print_wrap(line, 70)
}
}