ADT,,111111111,,*SOH,5.00,,,,,,,,# ADS,,222222222,,*SOH,1312.00,,,,,,,,# ADS,,111111111,,*FWT,14.00,,,,,,,,# ADS,,222222222,,*UNLF,99.75,,,,,,,,# #### 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; } } } #### ADS 222222222 *SOH # ADS,,222222222,,*SOH,,, ADS 111111111 *FWT # ADS,,111111111,,*FWT,,, ADS 222222222 *UNLF # ADS,,222222222,,*UNLF,,,