Last active
August 29, 2015 14:05
-
-
Save robmiller/0243ea79350c339e7e2a to your computer and use it in GitHub Desktop.
rows2cols — transpose rows of text into columns, with custom separators. e.g. `rows2cols -c 3 -s ' '` will convert "foo\nbar\nbaz\nfoo\nbar\nbaz" to "foo bar baz\nfoo bar baz". Works on files and standard input
This file contains 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 | |
def run | |
ARGF.each_slice($options[:columns]) do |values| | |
puts values.map(&:chomp).join($options[:separator]) | |
end | |
end | |
require "optparse" | |
$options = { columns: 4, separator: " " } | |
OptionParser.new do |opts| | |
opts.banner = "Usage: rows2cols [options] [file, file...]" | |
opts.on("-c", "--columns N", Integer, "Number of columns") do |columns| | |
$options[:columns] = columns | |
end | |
opts.on("-s", "--separator CHAR", "Separator character between columns") do |separator| | |
$options[:separator] = separator | |
end | |
opts.on("-h", "--help", "Display this help") do | |
puts opts | |
exit | |
end | |
end.parse! | |
run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment