my $out = "Rows_of_interest.txt"; open (OUT, "+>$out"); open my $fh, "<", "gridall.csv" or die "gridall.csv: $!\n"; my $all_lines; my $matched_line_count; #my $csv; while (my $row = my $csv->getline ($fh)) { $all_lines++; $row->[8] =~ m/^CE$/ or next; print OUT $row; $matched_line_count++; # do something with this line } $csv->eof or $csv->error_diag (); print "\n\nNumber of lines detected:\t"; print $all_lines; print "\n\n"; print "\n\nNumber of matched lines detected:\t"; print $matched_line_count; print "\n\n"