-
Notifications
You must be signed in to change notification settings - Fork 16
/
namecut
executable file
·40 lines (34 loc) · 875 Bytes
/
namecut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/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