#!/usr/bin/perl use warnings; use strict; use Text::CSV; open my $LST, '<', 'ids.lst' or die $!; my %id; while (<$LST>) { chomp; $id{$_} = $.; } my @out; my $csv = 'Text::CSV'->new({ sep_char => "\t", eol => "\n", }); open my $CSV, '<', 'file.csv' or die $!; while (my $row = $csv->getline($CSV)) { push @out, [ $id{ $row->[0] }, $row ] if exists $id{ $row->[0] }; } $csv->print(*STDOUT, $_->[1]) for sort { $a->[0] <=> $b->[0] } @out;