my @tokens; while () { if (/\.\.(AU|BD):/) { if ($1 eq 'AU') { print join( ',',@tokens),"\n", @tokens = () if @tokens; } chomp (my $line = ); push @tokens, $line; } } print join( ',',@tokens),"\n", @tokens = () if @tokens;