open my $FH, '<', './sub/100087.txt' or die "Cannot open './sub/100087.txt' because: $!"; my $count = 1; while ( <$FH> ) { next unless /^[a-z]/; chomp; print; print $count++ % 7 && !eof( $FH ) ? ", " : "\n"; }