__DATA__ blah blah blah blah blah blah blah blah blah blah blah blah INTERESTING CODE-- CODE NAME CODE NAME -------- ----------------------- -------- ----------------------- ABC NAME ONE RST NAME EIGHT ... DEF NAME TWO THREE WXY NAME NINE - TEN GHIJK NAME FOUR ... ZAB NAME ELEVEN LMN NAME FIVE - SIX CDE NAME TWELVE OPQ NAME SEVEN more blah blah blah #### ######################### print $interesting_code; # output "NAME ONE, NAME TWO THREE, NAME FOUR ..., NAME FIVE - SIX, NAME SEVEN, NAME EIGHT ..., NAME NINE - TEN, NAME ELEVEN, NAME TWELVE" # you get the picture... a csv list of NAMEs ######################### #### my $stuff; my @stuff; while () { if ($stuff) { push(@stuff, $_) if (!($_ =~ /^\s*CODE\s*NAME/) && !($_ =~ /^\s*-+/)); } else { $stuff = 1 if ($_ =~ /^INTERESTING CODE-+/); } } my @interesting_code; for (@stuff) { my @code = split(/\s{2,}/, $_); my $i = 0; for (@code) { push(@interesting_code, $_) if !($i % 2); $i++; } } $interesting_code = join(', ', @interesting_code);