#!/usr/bin/env ruby require 'trollop' STDIN.set_encoding 'utf-8' STDOUT.set_encoding 'utf-8' cfg = Trollop::options do banner "splitpipes -f < " opt :field, "field", :type => :int, :required => true opt :to, "to", :type => :int, :default => nil end a = [] range = false if cfg[:to] range = true end if range if cfg[:field] >= cfg[:to] STDERR.write "field >= to, exiting\n" exit end end if cfg[:field]<=0 || (range && cfg[:to]<=0) STDERR.write "field or to <= 0, exiting" exit end while line = STDIN.gets j = 1 line.strip.split(' ||| ').each { |i| if range && (cfg[:field]..cfg[:to]).include?(j) a << i.strip elsif j == cfg[:field] puts i.strip break end j += 1 } if range puts "#{a.join " ||| "}\n" end a.clear end