use warnings; use strict; use Data::Dumper; # Debug $Data::Dumper::Useqq=1; use Text::CSV; my $file1 = 'in1.txt'; my $file2 = 'in2.txt'; my $outfile = 'out.txt'; my $csv = Text::CSV->new({binary=>1, auto_diag=>2, eol=>$/}); open my $ifh1, '<', $file1 or die "$file1: $!"; open my $ifh2, '<', $file2 or die "$file2: $!"; open my $ofh, '>', $outfile or die "$outfile: $!"; $csv->getline($ifh1); # read and discard header $csv->getline($ifh2); # read and discard header $csv->print($ofh, ['col1','col2']); # new header while ( my $row1 = $csv->getline($ifh1) ) { my $row2 = $csv->getline($ifh2); die "file1 has more lines than file2" unless $row2; # do whatever you like to create the output row here my $orow = [@$row1, @$row2]; # example: join the two rows print Dumper($row1, $row2, $orow); # Debug $csv->print($ofh, $orow); } die "file2 has more lines than file1" unless eof($ifh2); close $ifh1; close $ifh2; close $ofh; $csv->eof or $csv->error_diag;