Create a gist now

Instantly share code, notes, and snippets.

@zunda /each-block.rb
Last active Feb 16, 2016

Run a command on each block in input file
#!/usr/bin/ruby
# usage: ruby each-block.rb sertificate-bundle.crt openssl x509 -noout -text
require 'open3'
src = ARGV.shift
cmd = ARGV
input = File.read(src)
blocks = input.scan(/(^-+BEGIN (\w+)-+\R.*?\R-+END (\2)-+\R)/m).map{|e| e[0]}
blocks.each do |block|
Open3.pipeline_w(cmd) do |i|
i.print block
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment