#!/usr/bin/awk -f # Cut just the columns specified. # usage awk -f columns.awk columns=one,two,five [header=2] [filenames] # The input must contain a header line. # This header contains column titles. # All lines before the header are ignored. # Eric Blossom - December 2003 BEGIN { header = 1; FS = "\t"; OFS = FS; } 1 == NR { temp = $0; n = split( columns, columnName, /[,;] */ ); numberOfColumnsOut = n; if ( 0 == n ) { print "ERROR: columns not defined." | "./tostderr"; exit 1; } $0 = temp; } FNR < header { next; } FNR == header { delete column; for ( i = 1; i <= NF; i++ ) { column[$i] = i; } next; } { n = 1; for ( i in columnName ) { if ( columnName[i] ) { value = ""; if ( columnName[i] in column ) { value = $(column[columnName[i]]); } printf value; } if ( n < numberOfColumnsOut ) { printf OFS; n++ } } print ""; }