Created
April 3, 2012 17:52
-
-
Save aef/2294143 to your computer and use it in GitHub Desktop.
A minimal command line tool to split multi-entity PEM files into a collection of files each containing only one entity.
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 | |
# encoding: UTF-8 | |
=begin | |
Copyright Alexander E. Fischer <aef@raxys.net>, 2012 | |
Permission to use, copy, modify, and/or distribute this software for any | |
purpose with or without fee is hereby granted, provided that the above | |
copyright notice and this permission notice appear in all copies. | |
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | |
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | |
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | |
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | |
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
PERFORMANCE OF THIS SOFTWARE. | |
=end | |
# A minimal command line tool to split multi-entity PEM files into a collection | |
# of files each containing only one entity. Requires Ruby >= 1.9.2. | |
require 'pathname' | |
∞ = Float::INFINITY | |
ARGV.map{|file| Pathname.new(file) }.each do |file| | |
file.open do |input_io| | |
1.upto(∞).each do |split_index| | |
split_file = Pathname.new(file.to_s + ".#{'%03d' % split_index}") | |
begin | |
while line = input_io.readline | |
output_io ||= split_file.open('w') | |
output_io << line | |
if line =~ /-----END [^\-]+-----/ | |
break | |
end | |
end | |
rescue EOFError | |
break | |
ensure | |
if output_io | |
output_io.close | |
output_io = nil | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment