Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 41 lines (34 sloc) 875 Bytes
#!/usr/bin/env ruby
$doc = %{
namecut [column names and/or 1-based indexes]
Assume first line is a header.
Those columns are extracted (like sql select).
Like 'cut' but with names; and jankier.
}
if STDIN.tty?
STDERR.puts $doc.strip
exit 1
end
colnames = $stdin.readline.chomp.split("\t")
cols = ARGV
problems = []
col_inds = cols.map { |c|
if c =~ /^\d+$/
c = c.to_i
raise "bad column number (1-indexed): #{c}" if c<=0 || c > cols.size+1
c -= 1
else
c = $1 if c =~ /^-f(.*)/
raise "don't support cut's full -f syntax yet" if c =~ /,/
colnames.index(c) or problems << c
end
}
if problems.size > 0
problems.each{|problem| $stderr.puts "No column with name: #{problem}"}
exit -1
end
puts col_inds.map{|i| colnames[i]}.join("\t")
$stdin.each do |line|
parts = line.chomp.split("\t")
puts col_inds.map{|i| parts[i]}.join("\t")
end