summaryrefslogtreecommitdiff
path: root/split-pipes
blob: 862e8bee20e335f266d6262431b8a262ddf4609a (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby

require 'optimist'

STDIN.set_encoding 'utf-8'
STDOUT.set_encoding 'utf-8'

conf = Optimist::options do
  banner "splitpipes -f <n> < <input>"
  opt :field, "field", :type => :int, :required => true
  opt :to,    "to",    :type => :int, :default => nil
end


a = []
range = false
if conf[:to]
  range = true
end

if range
  if conf[:field] >= conf[:to]
    STDERR.write "field >= to, exiting\n"
    exit
  end
end

if conf[:field]<=0 || (range && conf[:to]<=0)
  STDERR.write "field or to <= 0, exiting"
  exit
end

while line = STDIN.gets
  j = 1
  line.strip.split(' ||| ').each { |i|
    if range && (conf[:field]..conf[:to]).include?(j)
      a << i.strip
    elsif j == conf[:field] 
      puts i.strip
      break
    end
    j += 1
  }
  if range
    puts "#{a.join " ||| "}\n"
  end
  a.clear
end