#!/usr/bin/env ruby #!/usr/local/bin/ruby # Cut just the columns specified. # usage ruby -s columns.rb -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 - September 2004 header = 1 header = $header.to_i() if $header != nil columns = "" columns = $columns if $columns != nil columnTitle = columns.split( /[,;] */ ) $; = "\t" while gets line = chomp.split() next if $. < header if header == $. iCols = columnTitle.map { |x| line.index( x ) } iCols.delete( nil ) end out = iCols.map { |x| line[x] } puts out.join( $; ) if ! out.empty? end