Created
December 9, 2011 01:35
-
-
Save adparadise/1449691 to your computer and use it in GitHub Desktop.
A simple script to interleave several files, which may be very large
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
filenames = ARGV | |
usage = "USAGE: interleave.rb <filename> ..." | |
unless filenames.length > 0 | |
STDERR.write(usage + "\n") | |
exit(1) | |
end | |
// Open streams to all files | |
files = filenames.map do |filename| | |
fd = IO.sysopen(filename, "r") | |
IO.new(fd, "r") | |
end | |
// Emit the files line by line, interleaved. | |
while(files.any? { |file| !file.eof? }) do | |
files.each do |file| | |
next if file.eof? | |
line = file.gets | |
STDOUT.write(line); | |
end | |
end | |
// Close the handles. | |
files.each { |file| file.close } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment