#!/usr/bin/awk -f # Cut just the columns specified. # usage awk -f rproj.awk columns=one,two,five [filenames] # The first line of input must be a header line. # This header contains column titles. # Eric Blossom - May 2007 function assert(condition, message) { if (! condition) { printf("ERROR: %s:%d: %s\n", FILENAME, FNR, message) > "/dev/stderr" _assert_exit = 1 exit 1 } } BEGIN { FS = "\t"; OFS = FS; } 1 == NR { # Header (column meta data, name[[: domain]: type]) numberOfColumnsOut = split( columns, columnName, /[,;] */ ); assert( numberOfColumnsOut, "The variable \"columns\" is not defined." ) 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. } else { columnNumber[$i] = i } } } { n = 1; for ( i in columnName ) { assert( columnNumber[columnName[i]], "No column is named \"" columnName[i] "\"." ) printf $columnNumber[columnName[i]]; if ( n < numberOfColumnsOut ) { printf OFS; n++ } } print ""; }