#!/usr/bin/perl -w use strict; open my $csv1, "csv3.csv"; my %csv1_data; while (<$csv1>) { if (/^(?([^,]*,){3})(?([^,]*,){3}).*$/) { $csv1_data{$+{first}}=$+{fields}; } } while (<$csv2>) { if (/^(?([^,]*,){3})(?([^,]*,){3})(?.*)$/) { if (defined $csv1_data{$+{first}}) { print $csv3 "$+{first}$csv1_data{$+{first}}$+{rest}\n"; } else { print $csv3 $_; } } }