use warnings; use strict; use DBI; open (OUT, ">file.txt"); my @chk = (111111111,222222222); my $dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\,}); $dbh->{'csv_tables'}->{'comp'} = { 'file' => 'DUDS.csv'}; my $sql; my @row ; my $sth = $dbh->prepare("SELECT * from comp"); $sth->execute(); while (my @row = $sth->fetchrow_array) { foreach my $chk(@chk) { if ( $chk == $row[2]) { print OUT "@row\n"; print OUT "$row[0],$row[1],$row[2],$row[3],$row[4],$row[5],$row[6],$row[7]\n\n"; last; } } }