#!perl -slw use strict; my @lines; my $buffer; while( ){ chomp; $buffer .= $_; #! Accumulate in buffer if ($buffer =~ /^(cn=.+?)cn=/) { #! When we have a full line+ push @lines, $1; #! save it substr($buffer, 0, $+[1], ''); #! and strip from the buffer } } push@lines,$buffer; print for @lines;