#!/usr/bin/env python # Cut just the columns specified. # usage: python columns.py "col1,col2" [filenames] # The input must contain a header line. # This header contains column titles. # All lines before the header are ignored. # Eric Blossom - October 2004 # BUGS: # How do we optionally specify header line on the command line? # Can we use keywords on the command line? e.g. -columns=one,two -header=3 from fileinput import input import string import sys headerLine = 1 columns = "" nargs = len( sys.argv ) if nargs < 1: print >>sys.stderr, 'usage: python columns.py "col1,col2" [filenames]' else: columns = sys.argv[1] del sys.argv[1] columnTitle = string.split( columns, "," ) FS = "\t" lineNumber = 0 for line in input(): line = string.strip( line ) field = line.split() lineNumber += 1 if headerLine == lineNumber: # then we expect the column titles. iCols = [ field.index( x ) for x in columnTitle if x in field ] if len( iCols ) < 1: print >> sys.stderr, 'Whoa: Seems we are missing a header line.' out = [ field[x] for x in iCols ] if 0 < len( out ): print string.joinfields( out, FS )