hank (owner)

Revisions

gist: 198622 Download_button fork
public
Description:
A totally awesome Ruby script for concatenating files together in columns
Public Clone URL: git://gist.github.com/198622.git
Embed All Files: show embed
Ruby #
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
#!/usr/bin/ruby
# Original credits to Geet Duggal
# Concatenates an arbitrary number of files together into columns
# Columns map to lines in each file.
# Requirement: Files be of the same length.
# Oh, and cheers!
require "pp"
 
filez = ARGV.map do |fname|
  begin
    File.open(fname)
  rescue
    puts $!
    exit
  end
end
 
# Make sure all files have the same number of lines
num_lines = 0
num_lines = filez.map{ |f| f.readlines.length }.uniq
filez.each{|f| f.rewind }
if num_lines.length == 1
    num_lines = num_lines[0]
else
    puts "All files need to have the same number of lines"
    Process.exit(1)
end
 
# Go ahead and actually perform concatenation
while num_lines > 0
    puts filez.map{ |file| file.readline.chomp.split("\t") }.flatten.join("\t")
    num_lines -= 1
end