use strict; use warnings; use Text::CSV; my $out = "Rows_of_interest.txt"; open (OUT, "+>$out"); open my $fh, "<", "gridall.csv" or die "gridall.csv: $!\n"; my $all_lines = 0; my $matched_line_count = 0; my $csv = new Text::CSV(); while (my $row = $csv->getline($fh)) { $all_lines++; $row->[8] =~ m/^CE$/ or next; print OUT $row; $matched_line_count++; } $csv->eof or $csv->error_diag (); print "\n\nNumber of lines detected:\t$all_lines\n\n"; print "\n\nNumber of matched lines detected:\t$matched_line_count\n\n"; __END__ Output is: Number of lines detected: 1 Number of matched lines detected: 1