#!/usr/bin/env perl use strict; use utf8; use warnings; my $cert = ""; my $count = 0; # Loop over STDIN to get the nth certificate while () { # Append current line to potential cert $cert .= $_; # Check for end of certificate if ($_ =~ /^\-+END(\s\w+)?\sCERTIFICATE\-+$/) { # Print out the cert if this is the index we're looking for if (grep(/^$count$/, @ARGV)) { print $cert; } # Reset potential cert $cert = ""; $count++; } }