summaryrefslogtreecommitdiff
path: root/select-from
blob: 0ccfeaceddb2e3606190c9669d24f7315aa894c1 (plain)
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
#!/usr/bin/env ruby

require 'optimist'
require 'zipf'

opts = Optimist::options do
  banner "select_from [--invert] -i <file> < <line separated data>"
  opt :index,  "Line numbers to output.", :type => :string, :short => '-i', :required => true
  opt :invert, "Invert selection.", :type => :bool, :short => '-j', :default => false
  opt :from1,  "Index starting from 1.", :type => :bool, :short => '-k', :default => false
end

accept = {}
f = File.open  opts[:index]
f.each_line { |line|
  i = line.strip.to_i

  accept[i] = true
}

i=0
if opts[:from1]
  i = 1
end
while line = STDIN.gets
  if accept[i] && !opts[:invert]
    STDOUT.write line
  elsif !accept[i] && opts[:invert]
    STDOUT.write line
  end
  i += 1
end