#!/usr/bin/awk -f function assert(condition, message) { if (! condition) { printf("ERROR: %s:%d: %s\n", FILENAME, FNR, message) > "/dev/stderr" _assert_exit = 1 exit 1 } } function size(array, c, x) { c = 0 for (x in array) c++ return c } BEGIN { FS = "\t" OFS = FS } 1 == FNR { assert(column, "Must define column.") assert(newname, "Must define newname.") for (i = 1; i <= NF; i++ ) { n = split( $i, a, /: */ ) if ( 1 < n ) { # then column has a data type. columnNumber[a[1]] = i # Just get the name. p = index( $i, ":" ) type[$i] = substr($i, p) # Save the domain and type. } else { columnNumber[$i] = i type[$i] = "" } attribute[$i] = $i } assert(size(columnNumber) == NF, "Column names are not unique.") assert(size(attribute) == NF, "BUG: types and column names do not match.") assert(column in columnNumber, "Column " column " not found.") col = columnNumber[column] $col = newname type[$col] } { assert(size(columnNumber) == NF, "Not a relation. Row has " NF " fields. Expected " size(columnNumber) ".") print }