Note: You are viewing an old version of this page. View the current version.

#!/usr/bin/env ruby

def pebkac(msg = nil)
  me = File.basename($0)
  STDERR.printf("%s: %s\n\n", me, msg) if msg
  STDERR.puts "Interleaves two files line-by-line."
  STDERR.puts "Writes combined file to STDOUT."
  exit(1)
end

pebkac if ARGV.length < 2

src1 = ARGV.shift
src2 = ARGV.shift

pebkac('%s: no such file or directory' % src1) unless File.readable?(src1)
pebkac('%s: no such file or directory' % src2) unless File.readable?(src2)

f1 = File.open(src1, 'r')
f2 = File.open(src2, 'r')

while !f1.eof || !f2.eof
  s1 = f1.gets
  s2 = f2.gets
  STDOUT.puts(s1) unless s1.nil?
  STDOUT.puts(s2) unless s2.nil?
end

STDOUT.flush
f2.close
f1.close